1 / 40

Graph Introduction , Searching

Graph Introduction , Searching. Graph Theory Basics. - Anil Kishore. Graph. A collections of objects and pair wise relations between them A mathematical structure Objects are called Vertices ( or Nodes ) Relationship is shown using Edges. Graph. 2. 5. 1. 4. 6. 3.

eldon
Download Presentation

Graph Introduction , Searching

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. Graph Introduction , Searching Graph Theory Basics - Anil Kishore

  2. Graph • A collections of objects and pair wise relations between them • A mathematical structure • Objects are called Vertices ( or Nodes ) • Relationship is shown using Edges

  3. Graph 2 5 1 4 6 3 Vertex Set V = { 1, 2, 3, 4, 5, 6} Edge Set E = { 12, 14, 34, 45, 46, 25, 56 } We use N and M for the corresponding sizes |V| = N , |E| = M

  4. Storing a Graph • How to store a Graph ? The two popular representations are • Adjacency Matrix • N x N matrix A of 0s and 1s • Adjacency List • A list of neighbors for each of the vertices

  5. Adjacency Matrix 2 5 1 4 6 3 A[u][v] = 1, if vertex u and vertex v are adjacent = 0, otherwise A Symmetric Matrix Space : O(N2)

  6. Adjacency List 2 5 1 4 6 3 1 : { 2, 4 } 2 : { 1, 5 } 3 : { 4 } 4 : { 1, 3, 5, 6 } 5 : { 2, 4, 6 } 6 : { 4, 5 } Vertex u : list of all neighbors of u Space : O(N+2M)

  7. Directions and Weights • Edges can have direction • ( like one-way roads ) 2 5 1 4 6 7 2 5 3 3 2 5 1 1 6 4 6 • Edges and Vertices can have weight • e.g.: length of the road, • toll gate charge in a city. 4 3

  8. Path, Cycle 2 5 • A Path of length n-1 is a sequence of vertices u1, u2, … , unsuch that vertices ui and ui+1 are adjacent • e.g. 1 – 2 – 5 – 4 – 3 is a path 1 4 6 3 • A Cycleof length n is a sequence of vertices u1, u2, … , unsuch that vertices ui and ui+1 are adjacent and also u1 and un adjacent • e.g. 1 – 2 – 5 – 4 is a cycle

  9. Power of Adjacency Matrix • Number of paths ( possibly cyclic ) of length K from u to v F(u, v, K) = Sum of ( F(u, w, k-1) * F(w, v, 1) ) ( for all possible intermediate vertices w ) This is similar to the only computation step in Matrix Multiplication • Note that F(u, v, 1) = A[u][v] • AK[u][v] = Number of paths of length exactly K • from u to v

  10. Connected, Tree, Complete • A graph is said to be connected if there exists a path between all pairs of vertices • How many minimum edges we need to make a n vertices graph connected ? • Cycles introduce redundant edges • A Tree is a connected graph with out cycles ( acyclic ) • A tree on n vertices has exactly (n-1) edges • A Complete Graph has all possible edges present in it • A complete on n vertices (Kn) has nC2 edges

  11. Traversing a graph • Visit all the vertices in the graph in a particular order • Depth-first Search (DFS) • visit child nodes before visiting sibling nodes • Breadth-first Search (BFS) • visit sibling nodes before visiting child nodes

  12. Depth-first Search algorithm DFS( u ) // start time of u Mark u as ‘visited’ FOR each node v ∈ Adj.List(u) IF NOT visited(v) THEN par[v] := u DFS(v) ENDIF ENDFOR //end time of u END-DFS • Visit an unvisited neighbor, thus recursively traverse along depth of the graph • par[v] denotes the first preceding vertex from which vertex v was visited, and defines a DFS tree • Applications : • Checking connectivity • Finding Connected Components • Topological ordering • many more…

  13. DFS 5 2 1 4 6 3

  14. DFS 5 2 1 4 6 3

  15. DFS 5 2 1 4 6 3

  16. DFS 5 2 1 4 6 3

  17. DFS 5 2 1 4 6 3

  18. DFS 5 2 1 4 6 3

  19. DFS 5 2 1 4 6 3

  20. DFS 5 2 1 4 6 3

  21. DFS 5 2 1 4 6 3

  22. DFS 5 2 1 4 6 3

  23. DFS 5 2 1 4 6 3

  24. DFS 5 2 1 4 6 3

  25. DFS 5 2 1 4 6 3 DFS TREE

  26. DFS • Recursive algorithm • The active nodes in the recursion are Pushed and Popped, similar to a Stack • Instead of recursion, can implement using a Stack data structure • Complexity : O( N + M )

  27. Breadth-first Search algorithm BFS( s ) Mark all vertices u ‘unvisited’ Create an empty queue Q EnQueue(s, Q) mask s as ‘visited’ WHILE NOT Empty(Q) DO u:= DeQueue(Q) FOR each v ∈ Adj.List(u) DO IF NOT visited(v) EnQueue(v, Q) mask v as ‘visited’ ENDFOR ENDWHILE End-BFS • Visit the vertices in the order encountered • Vertices nearer to s are processed before farther ones • Applications : • Checking connectivity • Finding Connected Components • Shortest path in unweighted graphs • many more…

  28. BFS 5 2 1 4 6 3 Q :

  29. BFS 5 2 1 4 6 3 Q : 1

  30. BFS 5 2 1 4 6 3 Q : u = 1

  31. BFS 5 2 1 4 6 3 Q : 2, 4 u = 1

  32. BFS 5 2 1 4 6 3 Q : 4 u = 2

  33. BFS 5 2 1 4 6 3 Q : 4, 5 u = 2

  34. BFS 5 2 1 4 6 3 Q : 5 u = 4

  35. BFS 5 2 1 4 6 3 Q : 5, 3, 6 u = 4

  36. BFS 5 2 1 4 6 3 Q : 3, 6 u = 5

  37. BFS 5 2 1 4 6 3 Q : 6 u = 3

  38. BFS 5 2 1 4 6 3 Q : u = 6

  39. BFS 2 1 5 2 0 s 1 4 6 3 • Visit the nodes level by level ( level order traversal ) • All nodes at level k are the ones with • shortest path to s equals to k • Complexity : O( N + M )

  40. References • Introduction to Algorithms • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein • www.derekroconnor.net/home/MMS406/ - End -

More Related