1 / 57

CSE 2331/5331

CSE 2331/5331. Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort. a. b. c. d. e. f. What Is A Graph. Graph G = (V, E) V: set of nodes E: set of edges. Example: V ={ a, b, c, d, e, f }

Download Presentation

CSE 2331/5331

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. CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort CSE 2331/5331

  2. a b c d e f What Is A Graph • Graph G = (V, E) • V: set of nodes • E: set of edges • Example: • V ={ a, b, c, d, e, f } • E ={(a, b), (a, d), (a, e), (b, c), (b, d), (b, e), (c, e), (e,f)} CSE 2331/5331

  3. a b c d e f • Un-directed graph • Directed graph CSE 2331/5331

  4. Un-directed graphs CSE 2331/5331

  5. a b c d e f Vertex Degree • deg(v) = degree of v = # edges incident on v • Lemma: CSE 2331/5331

  6. Some Special Graphs • Complete graph • Path • Cycle • Planar graph • Tree CSE 2331/5331

  7. Representations of Graphs • Adjacency lists • Each vertex u has a list, recording its neighbors • i.e., all v’s such that (u, v)  E • An array of V lists • V[i].degree = size of adj list for node • V[i].AdjList= adjacency list for node CSE 2331/5331

  8. Adjacency Lists • For vertex v  V, its adjacency list • has size: deg(v) • decide whether (v, u)  E or not in time O(deg(v)) • Size of data structure (space complexity): • O(|V| + |E|) = O(V+E) CSE 2331/5331

  9. Adjacency Matrix • matrix • if is an edge • Otherwise, CSE 2331/5331

  10. Adjacency Matrix • Size of data structure: • O ( V  V) • Time to determine if (v, u)  E : • O(1) • Though larger, it is simpler compared to adjacency list. CSE 2331/5331

  11. Sample Graph Algorithm • Input: Graph G represented by adjacency lists Running time: O(V + E) CSE 2331/5331

  12. Connectivity • A path in a graph is a sequence of vertices such that there is an edge between any two adjacent vertices in the sequence • Two vertices are connected if there is a path in G from u to w. • We also say that w is reachable from u. • A graph G is connected if every pair of nodes are connected. CSE 2331/5331

  13. Connectivity Checking • How to check if the graph is connected? • One approach: Graph traversal • BFS: breadth-first search • DFS: depth-first search CSE 2331/5331

  14. BFS: Breadth-first search • Input: • Given graph G = (V, E), and a source node s  V • Output: • Will visit all nodes in V reachable from s • For each output a value • distance (smallest # of edges) from s to v. • if v is not reachable from s. CSE 2331/5331

  15. Intuition • Starting from source node s, • Spread a wavefront to visit other nodes • First visit all nodes one edge away from s • Then all nodes two edges away from s • … Need a data-structure to store nodes to be explored. CSE 2331/5331

  16. Intuition cont. • A node can be: • un-discovered • discovered, but not explored • explored (finished) • : • is set when node v is first discovered. • Need a data structure to store discovered but un-explored nodes • FIFO ! Queue CSE 2331/5331

  17. Pseudo-code Use adjacency list representation Time complexity: O(V+E) CSE 2331/5331

  18. Correctness of Algorithm • A node not reachable from s will not be visited • A node reachable from s will be visited • computed is correct: • Intuitively, if all nodes k distance away from s are in level k, and no other nodes are in level k, • Then all nodes (k+1)-distance away from s must be in level (k+1). • Rigorous proof by induction CSE 2331/5331

  19. Connectivity-Checking Time complexity: O(V+E) CSE 2331/5331

  20. Summary for BFS • Starting from source node s, visits remaining nodes of graph from small distance to large distance • This is one way to traverse an input graph • With some special property where nodes are visited in non-decreasing distance to the source node s. • Return distance between s to any reachable node in time O(|V| + |E|) CSE 2331/5331

  21. DFS: Depth-First Search • Another graph traversal algorithm • BFS: • Go as broad as possible in the algorithm • DFS: • Go as deep as possible in the algorithm CSE 2331/5331

  22. Example • Perform DFS starting from • What if we add edge CSE 2331/5331

  23. DFS • Time complexity • O(V+E) CSE 2331/5331

  24. Depth First Search Tree • If v is discovered when exploring u • Set • The collection of edges • form a tree, • called Depth-first search tree. CSE 2331/5331

  25. DFS with DFS Tree CSE 2331/5331

  26. Example • Perform DFS starting from CSE 2331/5331

  27. Another Connectivity Test CSE 2331/5331

  28. Traverse Entire Graph CSE 2331/5331

  29. Remarks • DFS(G, k) • Another way to compute all nodes reachable to the node • Same time complexity as BFS • There are nice properties of DFS and DFS tree that we are not reviewing in this class. CSE 2331/5331

  30. Directed Graphs CSE 2331/5331

  31. a b c d e f • Un-directed graph • Directed graph • Each edge is directed from u to v CSE 2331/5331

  32. a b c d e f Vertex Degree • indeg(v) = # edges of the form • outdeg(v) = # edges of the form • Lemma: • Lemma: CSE 2331/5331

  33. Representations of Graphs • Adjacency lists • Each vertex u has a list, recording its neighbors • i.e., all v’s such that (u, v)  E • An array of V lists • V[i].degree = size of adj list for node • V[i].AdjList= adjacency list for node CSE 2331/5331

  34. Adjacency Lists • For vertex v  V, its adjacency list • has size: outdeg(v) • decide whether (v, u)  E or not in time O(outdeg(v)) • Size of data structure (space complexity): • O(V+E) CSE 2331/5331

  35. Adjacency Matrix • matrix • if is an edge • Otherwise, CSE 2331/5331

  36. Adjacency Matrix • Size of data structure: • O ( V  V) • Time to determine if (v, u)  E : • O(1) • Though larger, it is simpler compared to adjacency list. CSE 2331/5331

  37. Sample Graph Algorithm • Input: Directed graph G represented by adjacency list Running time: O(V + E) CSE 2331/5331

  38. Connectivity • A path in a graph is a sequence of vertices such that there is an edge between any two adjacent vertices in the sequence • Given two vertices we say that wis reachable from u if there is a path in G from u to w. • Note: w is reachable from uDOES NOT necessarily mean that u is reachable from w. CSE 2331/5331

  39. Reachability Test • How many (or which) vertices are reachable from a source node, say ? CSE 2331/5331

  40. BFS and DFS • The algorithms for BFS and DFS remain the same • Each edge is now understood as a directed edge • BFS(V,E, s) : • visits all nodes reachable from s in non-decreasing order CSE 2331/5331

  41. BFS • Starting from source node s, • Spread a wavefront to visit other nodes • First visit all nodes one edge away from s • Then all nodes two edges away from s • … CSE 2331/5331

  42. Pseudo-code Use adjacency list representation Time complexity: O(V+E) CSE 2331/5331

  43. Number of Reachable Nodes Compute # nodes reachable from Time complexity: O(V+E) CSE 2331/5331

  44. DFS: Depth-First Search • Similarly, DFS remains the same • Each edge is now a directed edge • If we start with all nodes unvisited, • Then DFS(G, visits all nodes reachable to node • BFS from previous NumReachable() procedure can be replaced with DFS. CSE 2331/5331

  45. More Example • Is reachable from ? • Is reachable from ? CSE 2331/5331

  46. DFS above can be replaced with BFS DFS(G, k); CSE 2331/5331

  47. Topological Sort CSE 2331/5331

  48. Directed Acyclic Graph • A directed cycle is a sequence such that there is a directed edge between any two consecutive nodes. • DAG: directed acyclic graph • Is a directed graph with no directed cycles. CSE 2331/5331

  49. undershorts socks watch pants shoes shirt belt tie jacket Topological Sort • A topological sort of a DAG G = (V, E) • A linear ordering A of all vertices from V • If edge (u,v)  E => A[u] < A[v] CSE 2331/5331

  50. Another Example Why requires DAG? Is the sorting order unique? CSE 2331/5331

More Related