1 / 33

Graphs

Graphs. Chapter 15 explain graph-based algorithms Graph definitions Graph implementations. Graph Traversals. Data Structures and Other Objects Using C++. Graph Representation. A graph G = (V, E) V = set of vertices E = set of edges = subset of V  V Thus |E| = O(|V| 2 ).

ahudak
Download Presentation

Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphs • Chapter 15 explain graph-based algorithms • Graph definitions • Graph implementations. • Graph Traversals Data Structures and Other Objects Using C++

  2. Graph Representation • A graph G = (V, E) • V = set of vertices • E = set of edges = subset of V  V • Thus |E| = O(|V|2)

  3. Graph Variations • Variations: • A connected graphhas a path from every vertex to every other • In an undirected graph: • Edge (u,v) = edge (v,u) • No self-loops • In a directed graph: • Edge (u,v) goes from vertex u to vertex v, notated uv

  4. Graph Variations • More variations: • A weighted graph associates weights with either the edges or the vertices • e.g., a road map: edges might be weighted with distance • A hypergraph allows multiple edges between the same vertices • e.g., the call graph in a program (a function can get called from multiple points in another function)

  5. Graphs • We will typically express running times in terms of |E| and |V| • If |E|  |V|2the graph is dense • If |E|  |V| the graph is sparse • If you know you are dealing with dense or sparse graphs, different data structures may make sense

  6. Representing Graphs • Assume V = {1, 2, …, n} • An adjacency matrixrepresents the graph as a n x n matrix A: • A[i, j] = 1, if edge (i, j)  E = 0, if edge (i, j)  E • A[i, j] = weight on the edge, if edge (i, j)  E = 0, if edge (i, j)  E

  7. Graphs: Adjacency Matrix • Example: 1 a d 2 4 b c 3

  8. Graphs: Adjacency Matrix 1 • Example: a d 2 4 b c 3 • How much storage does the adjacency matrix require? • A: O(V2)

  9. Graphs: Adjacency Matrix • The adjacency matrix is a dense representation • Usually too much storage for large graphs • But can be very efficient for small graphs • Most large interesting graphs are sparse • e.g., planar graphs, in which no edges cross, have |E| = O(|V|) by Euler’s formula • For this reason the adjacency list is often a more appropriate representation

  10. Graphs: Adjacency List • Adjacency list: for each vertex v  V, store a list of vertices adjacent to v • Example: For an undirected graph

  11. Graphs: Adjacency List • Adjacency list: for each vertex v  V, store a list of vertices adjacent to v • Example: For a directed graph

  12. Graphs: Adjacency List • How much storage is required? • The degree of a vertex v = # of edges • Directed graphs have in-degree, out-degree • For directed graphs, # of items in adjacency lists is out-degree(v) = |E|takes O(V + E) storage • For undirected graphs, # items in adjacency lists is degree(v) = 2 |E| (handshaking lemma)also O(V + E) storage • So: Adjacency lists take O(V+E) storage

  13. Graph Traverse • Given: a graph G = (V, E), directed or undirected • Goal: methodically explore every vertex and every edge • Ultimately: build a tree on the graph • Pick a vertex as the root • Choose certain edges to produce a tree • Note: might also build a forest if graph is not connected

  14. Depth-First Search (DFS) • “Explore” a graph, turning it into a tree • One vertex at a time • Moving “deeper” from last visited vertex to unvisited one • Backtracks if no adjacent unvisited vertex • Uses a stack • 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

  15. Example: DFS

  16. a b c d e f g h Example: DFS DFS tree: a b DFS traversal stack: f g h8,3 d7,4 c6,5 g5,6 e4,1 f3,2 b2,7 a1,8 c e d h

  17. DFS: Kinds of edges • Tree edge • Back edge: from descendent to ancenstor • Forward edge: from ancenstor to descendent • Cross edge: between subtress

  18. Notes on DFS • DFS can be implemented with graphs represented as: • adjacency matrices: Θ(V2) • adjacency lists: Θ(|V|+|E|) • Yields two distinct ordering of vertices: • order in which vertices are first encountered (pushed onto stack) • order in which vertices become dead-ends (popped off stack) • Applications: • checking connectivity, finding connected components • checking acyclicity • finding articulation points and biconnected components • searching state-space of problems for solution (AI)

  19. Breadth-first search (BFS) • Visits graph vertices by moving across to all the neighbors of last visited vertex • Instead of a stack, BFS uses a queue • Similar to level-by-level tree traversal

  20. Example: BFS

  21. a b c d e f g h Example: BFS a BFS tree: b e f g BFS traversal queue: h c a1 b2 e3 f4 g5 c6 h7 d8 d

  22. Notes on BFS • BFS has same efficiency as DFS and can be implemented with graphs represented as: • adjacency matrices: Θ(V2) • adjacency lists: Θ(|V|+|E|) • Yields single ordering of vertices (order added/deleted from queue is the same) • Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges

  23. Directed Acyclic Graphs • A directed acyclic graph or DAG is a directed graph with no directed cycles:

  24. DAG Shortest Paths • Problem: finding shortest paths in DAG

  25. Dijkstra’s Algorithm • If no negative edge weights, Dijkstra’s essentially a weighted version of breadth-first search • Similar to breadth-first search • Grow a tree gradually, advancing from vertices taken from a queue

  26. Dijkstra’s Algorithm B 2 10 D A Dijkstra(G) for each v  V d[v] = ; d[s] = 0; S = ; Q = V; while (Q  ) u = ExtractMin(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); 4 3 5 1 C

  27. Dijkstra’s Algorithm B  2 10 D A s Dijkstra(G) for each v  V d[v] = ; d[s] = 0; S = ; Q = V; while (Q  ) u = ExtractMin(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); 0 4 3  5 1  C

  28. Dijkstra’s Algorithm B 10 2 10 D A s Dijkstra(G) for each v  V d[v] = ; d[s] = 0; S = ; Q = V; while (Q  ) u = ExtractMin(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); 0 4 3  5 1 5 C S = {A}

  29. Dijkstra’s Algorithm B 9 2 10 D A s Dijkstra(G) for each v  V d[v] = ; d[s] = 0; S = ; Q = V; while (Q  ) u = ExtractMin(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); 0 4 3 6 5 1 5 C S = {A, C}

  30. Dijkstra’s Algorithm B 8 2 10 D A s Dijkstra(G) for each v  V d[v] = ; d[s] = 0; S = ; Q = V; while (Q  ) u = ExtractMin(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); 0 4 3 6 5 1 5 C S = {A, C, D}

  31. Dijkstra’s Algorithm B 8 2 10 D A s Dijkstra(G) for each v  V d[v] = ; d[s] = 0; S = ; Q = V; while (Q  ) u = ExtractMin(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); 0 4 3 6 5 1 5 C S = {A, C, D, B}

  32. Dijkstra’s Algorithm B 8 2 10 D A s Dijkstra(G) for each v  V d[v] = ; d[s] = 0; S = ; Q = V; while (Q  ) u = ExtractMin(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); 0 4 3 6 5 1 5 C

  33. Dijkstra’s Algorithm Analyasis How many times is ExtractMin() called? Dijkstra(G) for each v  V d[v] = ; d[s] = 0; S = ; Q = V; while (Q  ) u = ExtractMin(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 V) using heap for Q What will be the total running time?

More Related