Graphs Chapter 15 explain graphbased algorithms p Graph
Graphs Chapter 15 explain graphbased algorithms p Graph definitions p Graph implementations. p Graph Traversals p Data Structures and Other Objects Using C++
Graph Representation p A graph G = (V, E) p V = set of vertices p E = set of edges = subset of V V p Thus |E| = O(|V|2)
Graph Variations p Variations: p A connected graph has a path from every vertex to every other p In an undirected graph: p Edge (u, v) = edge (v, u) p No self-loops p In a directed graph: p Edge u v (u, v) goes from vertex u to vertex v, notated
Graph Variations p More variations: p A weighted graph associates weights with either the edges or the vertices p e. g. , a road map: edges might be weighted with distance p A hypergraph allows multiple edges between the same vertices p e. g. , the call graph in a program (a function can get called from multiple points in another function)
Graphs p We will typically express running times in terms of |E| and |V| p If |E| |V|2 the graph is dense p If |E| |V| the graph is sparse p If you know you are dealing with dense or sparse graphs, different data structures may make sense
Representing Graphs p Assume V = {1, 2, …, n} p An adjacency matrix represents the graph as a n x n matrix A: p A[i, j] = 1, if edge (i, j) E = 0, if edge (i, j) E p A[i, j] = weight on the edge, if edge (i, j) E = 0, if edge (i, j) E
Graphs: Adjacency Matrix p Example: A 1 d b 4 c 3 2 3 1 a 2 1 2 3 4 ? ? 4
Graphs: Adjacency Matrix p Example: 1 a d 2 b 4 c 3 A 1 2 3 4 1 0 1 1 0 2 0 0 1 0 3 0 0 4 0 0 1 0 • How much storage does the adjacency matrix require? • A: O(V 2)
Graphs: Adjacency Matrix p The adjacency matrix is a dense representation p Usually too much storage for large graphs p But can be very efficient for small graphs p Most large interesting graphs are sparse p e. g. , planar graphs, in which no edges cross, have |E| = O(|V|) by Euler’s formula p For this reason the adjacency list is often a more appropriate representation
Graphs: Adjacency List p Adjacency list: for each vertex v V, store a list of vertices adjacent to v p Example: For an undirected graph
Graphs: Adjacency List p Adjacency list: for each vertex v V, store a list of vertices adjacent to v p Example: For a directed graph
Graphs: Adjacency List p How much storage is required? p The degree of a vertex v = # of edges p Directed graphs have in-degree, out-degree p For directed graphs, # of items in adjacency lists is out-degree(v) = |E| takes O(V + E) storage p For undirected graphs, # items in adjacency lists is degree(v) = 2 |E| (handshaking lemma) also O(V + E) storage p So: Adjacency lists take O(V+E) storage
Graph Traverse p Given: a graph G = (V, E), directed or undirected p Goal: methodically explore every vertex and every edge p Ultimately: build a tree on the graph p Pick a vertex as the root p Choose certain edges to produce a tree p Note: might also build a forest if graph is not connected
Depth-First Search (DFS) p “Explore” a graph, turning it into a tree p p One vertex at a time Moving “deeper” from last visited vertex to unvisited one Backtracks if no adjacent unvisited vertex Uses a stack p p A vertex is pushed onto the stack when it’s reached for the first time A vertex is popped off the stack when it become a dead end, i. e. , when there is no adjacent unvisited vertex
Example: DFS
Example: DFS a b c d DFS tree: a e f g h b DFS traversal stack: e 4, 1 f 3, 2 b 2, 7 a 1, 8 h 8, 3 d 7, 4 c 6, 5 g 5, 6 f e g c d h
DFS: Kinds of edges p Tree edge p Back edge: from descendent to ancenstor p Forward edge: from ancenstor to descendent p Cross edge: between subtress
Notes on DFS p DFS can be implemented with graphs represented as: p p p Yields two distinct ordering of vertices: p p p adjacency matrices: Θ(V 2) adjacency lists: Θ(|V|+|E|) order in which vertices are first encountered (pushed onto stack) order in which vertices become dead-ends (popped off stack) Applications: p p checking connectivity, finding connected components checking acyclicity finding articulation points and biconnected components searching state-space of problems for solution (AI)
Breadth-first search (BFS) p Visits graph vertices by moving across to all the neighbors of last visited vertex p Instead of a stack, BFS uses a queue p Similar to level-by-level tree traversal
Example: BFS
Example: BFS a b c d BFS tree: a e f g h b BFS traversal queue: a 1 b 2 e 3 f 4 g 5 c 6 h 7 d 8 e g c h d f
Notes on BFS p BFS has same efficiency as DFS and can be implemented with graphs represented as: p p adjacency matrices: Θ(V 2) adjacency lists: Θ(|V|+|E|) p Yields single ordering of vertices (order added/deleted from queue is the same) p Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges
Directed Acyclic Graphs p A directed acyclic graph or DAG is a directed graph with no directed cycles:
DAG Shortest Paths p Problem: finding shortest paths in DAG
Dijkstra’s Algorithm p If no negative edge weights, Dijkstra’s essentially a weighted version of breadthfirst search p Similar to breadth-first search p Grow a tree gradually, advancing from vertices taken from a queue
Dijkstra’s Algorithm B Dijkstra(G) for each v V 2 D A 10 d[v] = ; 4 3 d[s] = 0; S = ; Q = V; 5 1 while (Q ) C u = Extract. Min(Q); S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u, v)) d[v] = d[u]+w(u, v);
Dijkstra’s Algorithm B Dijkstra(G) for each v V 2 D s A 10 d[v] = ; 4 3 0 d[s] = 0; S = ; Q = V; 5 1 while (Q ) C u = Extract. Min(Q); S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u, v)) d[v] = d[u]+w(u, v);
Dijkstra’s Algorithm B 10 Dijkstra(G) for each v V 2 D s A 10 d[v] = ; 4 3 0 d[s] = 0; S = ; Q = V; 5 1 5 while (Q ) C u = Extract. Min(Q); S = {A} S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u, v)) d[v] = d[u]+w(u, v);
Dijkstra’s Algorithm B 9 Dijkstra(G) for each v V 2 D s A 10 d[v] = ; 4 3 0 6 d[s] = 0; S = ; Q = V; 5 1 5 while (Q ) C u = Extract. Min(Q); S = {A, C} S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u, v)) d[v] = d[u]+w(u, v);
Dijkstra’s Algorithm B 8 Dijkstra(G) for each v V 2 D s A 10 d[v] = ; 4 3 0 6 d[s] = 0; S = ; Q = V; 5 1 5 while (Q ) C u = Extract. Min(Q); S = {A, C, D} S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u, v)) d[v] = d[u]+w(u, v);
Dijkstra’s Algorithm B 8 Dijkstra(G) for each v V 2 D s A 10 d[v] = ; 4 3 0 6 d[s] = 0; S = ; Q = V; 5 1 5 while (Q ) C u = Extract. Min(Q); S = {A, C, D, B} S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u, v)) d[v] = d[u]+w(u, v);
Dijkstra’s Algorithm B 8 Dijkstra(G) for each v V 2 D s A 10 d[v] = ; 4 3 0 6 d[s] = 0; S = ; Q = V; 5 1 5 while (Q ) C u = Extract. Min(Q); S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u, v)) d[v] = d[u]+w(u, v);
Dijkstra’s Algorithm Analyasis How many times is Dijkstra(G) Extract. Min() called? for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = Extract. Min(Q); S = S U {u}; for each v u->Adj[] if (d[v] > d[u]+w(u, v)) d[v] = d[u]+w(u, v); A: O(E lg be V) the using heaprunning for Q time? What will total
- Slides: 33