graph introduction, searching graph theory basics - anil kishore
TRANSCRIPT
![Page 1: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/1.jpg)
Graph Introduction , Searching
Graph Theory Basics
- Anil Kishore
![Page 2: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/2.jpg)
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
![Page 3: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/3.jpg)
Graph
1
6
5
4
3
2
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
![Page 4: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/4.jpg)
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
![Page 5: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/5.jpg)
Adjacency Matrix
1
6
5
4
3
2
1 2 3 4 5 6
1 1 1 0 1 0 0
2 1 1 0 0 1 0
3 0 0 1 1 0 0
4 1 0 1 1 1 1
5 0 1 0 1 1 1
6 0 0 0 1 1 1
A
A[u][v] = 1, if vertex u and vertex v are adjacent
= 0, otherwise
Space : O(N2)
Symmetric Matrix
![Page 6: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/6.jpg)
Adjacency List
1
6
5
4
3
2
1 : { 2, 4 }2 : { 1, 5 }3 : { 4 }4 : { 1, 3, 5, 6 }5 : { 2, 4, 6 }6 : { 4, 5 }
Space : O(N+2M)
Vertex u : list of all neighbors of u
![Page 7: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/7.jpg)
Directions and Weights
1
6
5
4
3
2
1
6
5
4
3
23
2
6
4
7
51
• Edges and Vertices can have weight e.g.: length of the road, toll gate charge in a city.
• Edges can have direction ( like one-way roads )
![Page 8: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/8.jpg)
Path, Cycle
• A Path of length n-1 is a sequence of vertices u1, u2, … , un such that vertices ui and ui+1 are adjacento e.g. 1 – 2 – 5 – 4 – 3 is a path
1
6
5
4
3
2
• A Cycle of length n is a sequence of vertices u1, u2, … , un such that vertices ui and ui+1 are adjacent and also u1 and un adjacento e.g. 1 – 2 – 5 – 4 is a cycle
![Page 9: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/9.jpg)
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 Kfrom u to v
![Page 10: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/10.jpg)
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
![Page 11: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/11.jpg)
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
![Page 12: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/12.jpg)
Depth-first Search
algorithm DFS( u )// start time of uMark u as ‘visited’FOR each node v Adj.List(∈ u)
IF NOT visited(v) THENpar[v] := uDFS(v)
ENDIFENDFOR//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…
![Page 13: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/13.jpg)
DFS
1
6
5
4
3
2
![Page 14: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/14.jpg)
DFS
1
6
5
4
3
2
![Page 15: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/15.jpg)
DFS
1
6
5
4
3
2
![Page 16: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/16.jpg)
DFS
1
6
5
4
3
2
![Page 17: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/17.jpg)
DFS
1
6
5
4
3
2
![Page 18: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/18.jpg)
DFS
1
6
5
4
3
2
![Page 19: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/19.jpg)
DFS
1
6
5
4
3
2
![Page 20: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/20.jpg)
DFS
1
6
5
4
3
2
![Page 21: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/21.jpg)
DFS
1
6
5
4
3
2
![Page 22: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/22.jpg)
DFS
1
6
5
4
3
2
![Page 23: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/23.jpg)
DFS
1
6
5
4
3
2
![Page 24: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/24.jpg)
DFS
1
6
5
4
3
2
![Page 25: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/25.jpg)
DFS
1
6
5
4
3
2
DFS TREE
![Page 26: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/26.jpg)
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 )
![Page 27: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/27.jpg)
Breadth-first Searchalgorithm BFS( s )
Mark all vertices u ‘unvisited’Create an empty queue QEnQueue(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’
ENDFORENDWHILE
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 graphsmany more…
![Page 28: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/28.jpg)
BFS
1
6
5
4
3
2
Q :
![Page 29: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/29.jpg)
BFS
1
6
5
4
3
2
Q : 1
![Page 30: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/30.jpg)
BFS
1
6
5
4
3
2
Q : u = 1
![Page 31: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/31.jpg)
BFS
1
6
5
4
3
2
Q : 2, 4u = 1
![Page 32: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/32.jpg)
BFS
1
6
5
4
3
2
Q : 4u = 2
![Page 33: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/33.jpg)
BFS
1
6
5
4
3
2
Q : 4, 5u = 2
![Page 34: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/34.jpg)
BFS
1
6
5
4
3
2
Q : 5u = 4
![Page 35: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/35.jpg)
BFS
1
6
5
4
3
2
Q : 5, 3, 6u = 4
![Page 36: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/36.jpg)
BFS
1
6
5
4
3
2
Q : 3, 6u = 5
![Page 37: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/37.jpg)
BFS
1
6
5
4
3
2
Q : 6u = 3
![Page 38: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/38.jpg)
BFS
1
6
5
4
3
2
Q : u = 6
![Page 39: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/39.jpg)
BFS
1
6
5
4
3
20
12
• 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 )
s
![Page 40: Graph Introduction, Searching Graph Theory Basics - Anil Kishore](https://reader036.vdocuments.mx/reader036/viewer/2022081503/5697bf8c1a28abf838c8bba1/html5/thumbnails/40.jpg)
References
• Introduction to Algorithms– Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
• www.derekroconnor.net/home/MMS406/
- End -