html5-img
1 / 29

Midterm Monday Oct . 20

Midterm Monday Oct . 20. Chapter 3 Decompositions of Graphs. Graphs. Graph Depth-first search in undirected graphs Depth-first search in directed graphs Forward edge Cross edge Back edge Strongly connected components Union-Find. Graph. Graph G = ( V , E ) V = set of vertices

Télécharger la présentation

Midterm Monday Oct . 20

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. Midterm Monday Oct. 20

  2. Chapter 3Decompositions of Graphs

  3. Graphs • Graph • Depth-first search in undirected graphs • Depth-first search in directed graphs • Forward edge • Cross edge • Back edge • Strongly connected components • Union-Find

  4. Graph • Graph G = (V, E) • V = set of vertices • E = set of edges  (VV)

  5. a b b d c a 1 2 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 a b a c b c d a b c d c d d a c 3 4 Representation of Graphs • Two standard ways. • Adjacency Lists. • Adjacency Matrix.

  6. a b b d c a c b c d c d d a b b d c a a c b c d a b c d d a c Adjacency Lists • Consists of an array Adj of |V| lists. • One list per vertex. • For uV, Adj[u] consists of all vertices adjacent to u. If weighted, store weights also in adjacency lists.

  7. 1 2 1 2 3 4 1 0 1 1 1 2 0 0 1 0 3 0 0 0 1 4 0 0 0 0 a b c d 4 3 1 2 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 a b c d 3 4 Adjacency Matrix • |V|  |V| matrix A. • Number vertices from 1 to |V| in some arbitrary manner. • A is then given by: A = AT for undirected graphs.

  8. Adjacency Matrix vs Adjacency List • Adjacency matrix uses fixed amount of space • Depends on number of vertices • Does not depend on number of edges • Presence of an edge between two vertices can be known immediately • All neighbors of a vertex found by scanning entire row for that vertex • Adjacency list represents only edges that originate from the vertex • Space not reserved for edges that do not exist • more often used than adjacency matrix

  9. DFS Depth-first search in undirected graphs

  10. DFS • Implementation • Stack • Cost? • O(|V|+|E|)

  11. DFS Depth-first search in directed graphs

  12. DFS

  13. DAG Directed acyclic graphs

  14. Strongly connected components Directed graph: u and v are connectediff there is a path from u to v and a path from v to u. 5 strongly connected components

  15. Strongly connected components • Undirected graph • DFS

  16. Project 2 – Percolation problem

  17. Union/Find Disjoint Sets Union/Find Algorithms

  18. Parent Pointer Array Implementation for a General Tree

  19. Implementation (cont.) The parent pointer representation is often used to maintain a collection of disjoint sets and support the two basic operations: • Check if two objects are in same set • Merge two sets

  20. I A B D F J C H E G Example: A Network Graph

  21. Find Root • Check if two elements are in same tree // Return TRUE if nodes in different trees boolean differ(int a, int b) { int root1 = find(a); // Find root for a int root2 = find(b); // Find root for b return root1 != root2; // Compare roots }

  22. F A G D B C I E H Merge Two Sets UNION/FIND

  23. Equiv Class Processing (1) (A,B) (C,H) (G,F) (D,E) (I,F) (H,A) (E,G)

  24. Equiv Class Processing (2) (H,E)

  25. Union/Find int find(int curr) { while (array[curr]!=ROOT) curr = array[curr]; return curr; // At root } void union(int a, int b) { int root1 = find(a); // Find root for a int root2 = find(b); // Find root for b if (root1 != root2) array[root2] = root1; } Want to keep the depth small. Weighted union rule: Join the tree with fewer nodes to the tree with more nodes.

  26. Path Compression • Find: • Return root of current node • Reset every node on the path from curr to the • root to point directly to the root.

  27. G D D G A A H B C E E F F I I H B C Path Compression (cont.) Find: • Return root of current node • Reset every node on the path from curr to the root to point directly to the root.

  28. D G A E F I H B C Path Compression (cont.) UNION:

More Related