CSE 373, Copyright S. Tanimoto, 2001 Graphs 2 - 1 Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Some Graph Algorithms.
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Algorithms and Data Structures
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graphs CS3240, L. grewe.
Graphs.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph & BFS.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
COMP171 Depth-First Search.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
 Last lesson  Graphs  Today  Graphs (Implementation, Traversal)
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Tree Searching Breadth First Search Dept First Search.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Chapter 2 Graph Algorithms.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSE 373, Copyright S. Tanimoto, 2002 Graphs 2 -
Graphs G = (V, E) V are the vertices; E are the edges.
Elementary Graph Algorithms
Presentation transcript:

CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix, adjacency lists. Graph search: depth-first, breadth-first. Shortest paths.

CSE 373, Copyright S. Tanimoto, 2001 Graphs Incidence and Adjacency Let e =  v 1, v 2  be an edge of a digraph. Then v 1 is adjacent to v 2. And v 2 is adjacent from v 1. e is incident to v 2. e is incident from v 1. For an undirected graph we can use the terms “adjacent to” and “incident to” for relationships in both directions. v2v2 v1v1 e

CSE 373, Copyright S. Tanimoto, 2001 Graphs Representing a Digraph with an Adjacency Matrix G =  V, E , V = { v 1, v 2,..., v n }, E = { e 1, e 2,..., e m } e i =  v a(i), v b(i)  An adjacency matrix for G contains n rows and n columns. A[i,j] = 1 if  v i, v j   E; 0 otherwise. a b c d a b c d c d a b

CSE 373, Copyright S. Tanimoto, 2001 Graphs Adjacency Lists Represent the adjacency matrix info as an array of lists, one for each row of the matrix. The i th list contains the vertices to which v i is adjacent. (Similar to the sparse array idea). a b c d a a: b, c, d b b: c c: d d d: c Efficent for getNeighbors(v)

CSE 373, Copyright S. Tanimoto, 2001 Graphs Incidence Matrix e 1 e 2 e 3 e 4 e 5 a b c d E = {  a, b ,  a, d ,  a, c ,  c, d ,  d, c  } -1: edge starts at that vertex (it’s incident from the vertex) 1: edge ends at that vertex (it’s incident to the vertex) 0: edge does not involve the vertex.

CSE 373, Copyright S. Tanimoto, 2001 Graphs Using a Hash Table If the graph is sparse, store adjacency info in the hash table. if  v i, v j   E, perform put(  v i, v j , 1) If the complementary graph is sparse (the graph is very dense), then store the complement of the edges in the hash table. if  v i, v j   E, perform put(  v i, v j , 0) In either case, the keys are edges, and the values tell whether or not the edge is part of the graph.

CSE 373, Copyright S. Tanimoto, 2001 Graphs Graph Search Given a directed graph G =  V,E  and a starting vertex s, find a path from s to some goal vertex g, where g is any vertex that satisfies a goal(v) predicate. E.g., let s = a. c d a b e f g

CSE 373, Copyright S. Tanimoto, 2001 Graphs Possible Solutions There may be 0 solutions, 1 solution, or many solutions. This particular problem seems to have at least 2 distinct solutions. P =   a,c ,  c,f ,  f,e ,  e,g  . c d a b e f g

CSE 373, Copyright S. Tanimoto, 2001 Graphs Some Additional Solutions If cycles are permitted in the path, there may be an infinite number of solutions. P =   a,d ,  d,c ,  c,d ,...,  d,c ,  c,f ,  f,e ,  e,g ,  g,f ,...,  f,e ,  e,g   c d a b e f g

CSE 373, Copyright S. Tanimoto, 2001 Graphs Avoiding Loops Even if cycles do not occur in the solution, they may cause a nuisance for any search process that is not prepared for them. Therefore, it is important to keep track of which vertices have been visited during a search. We’ll maintain two lists: OPEN and CLOSED. OPEN will hold the set of vertices that have been discovered so far but that have not yet been examined. CLOSED will hold the vertices that have already been examined.

CSE 373, Copyright S. Tanimoto, 2001 Graphs Depth-First Search Set OPEN =  S  ; Set CLOSED =   ; Initialize a hash table PRED. (for “predecessor”) Put into PRED: (S, NULL); While OPEN is not empty do { Remove the first element of OPEN; call this V. If Goal(V) then output the reverse of the linked list: V, PRED(V), PRED(PRED(V)), etc., and return. Find all W such that  V,W   E. Call this set L. Remove from L any elements already on OPEN or CLOSED. For each of the remaining elements V’ of L, put into PRED: (V’, V) and put V’ onto OPEN at the front of the list. Put V onto CLOSED. } Output “No solution” and return.

CSE 373, Copyright S. Tanimoto, 2001 Graphs Breadth-First Search Set OPEN =  S  ; Set CLOSED =   ; Initialize a hash table PRED. (for “predecessor”) Put into PRED: (S, NULL); While OPEN is not empty do { Remove the first element of OPEN; call this V. If Goal(V) then output the reverse of the linked list: V, PRED(V), PRED(PRED(V)), etc., and return. Find all W such that  V,W   E. Call this set L. Remove from L any elements already on OPEN or CLOSED. For each of the remaining elements V’ of L, put into PRED: (V’, V) and put V’ onto OPEN at the rear of the list. Put V onto CLOSED. } Output “No solution” and return.

CSE 373, Copyright S. Tanimoto, 2001 Graphs Comparison of DFS and B(readth)FS In DFS, OPEN serves as a Stack In BFS, OPEN serves as a Queue. In both, CLOSED serves as a Set. Both DFS and BFS find a solution if it exists. BFS always finds a shortest solution (if one exists). DFS finds some solution that depends upon the order in which successors (members of L) are determined and processed.

CSE 373, Copyright S. Tanimoto, 2001 Graphs Vertex Visitation Order In what order do vertices get processed (removed from OPEN and used as the value of V in the algorithm?) DFS: a, b, c, f, e, g BFS: a, b, c, d, f, e, g c d a b e f g