![Page 1: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/1.jpg)
GRAPH ALGORITHMNattee Niparnan
![Page 2: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/2.jpg)
Graph
A pair G = (V,E) V = set of vertices
(node) E = set of edges
(pairs of vertices)
V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),
(1,4),(4,5),(6,7))
1
2
3
45
67
![Page 3: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/3.jpg)
Term you should already know
directed, undirected graph Weighted graph Bipartite graph Tree
Spanning tree Path, simple path Circuit, simple circuit Degree
![Page 4: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/4.jpg)
Representing a Graph
Adjacency Matrix A = |V|x|V| matrix
axy = 1 when there is an edge connecting node x and node y
axy = 0 otherwise
1
2
3
45
0 1 0 1 0
1 0 1 1 0
0 1 0 0 1
1 1 0 0 1
0 0 1 1 0
1 2 3 4 5
123
45
![Page 5: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/5.jpg)
Representing a Graph
Adjacency List Use a list instead of a matrix For each vertex, we have a linked list of
their neighbor
1
2
3
45
1 2 4
2 1 3 4
. . .
![Page 6: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/6.jpg)
Representing a Graph
Incidences Matrix Row represent edge Column represent node
1
2
3
45
1 1 0 0 0
1 0 0 1 0
0 1 0 1 0
0 1 1 0 0
0 0 1 0 1
0 0 0 1 1
1 2 3 4 5
![Page 7: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/7.jpg)
Exploring a Maze
![Page 8: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/8.jpg)
Exploring Problem
Input: A Graph
Maybe as an adjacency matrix A Starting node v
Output: List of node reachable from v
Maybe as an array indexed by a node
![Page 9: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/9.jpg)
Depth-First-Search
procedure explore(G; v)// Input: G = (V;E) is a graph; v V// Output: visited(u) is set to true for all nodes u reachable from v{ visited(v) = true previsit(v) for each edge (v,u) E if not visited(u)
explore(u) postvisit(v)}
![Page 10: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/10.jpg)
Example
Explore(A)
![Page 11: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/11.jpg)
Extend to Graph Traversal
Traversal is walking in the graph We might need to visit each component
in the graph Can be done using explore
Do “explore” on all non-visited node The result is that we will visit every node
What is the difference between just looking into V (the set of vertices?)
![Page 12: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/12.jpg)
Graph Traversal using DFS
procedure dfs(G){
for all v V visited(v) = falsefor all v V if not visited(v)
explore(v)}
![Page 13: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/13.jpg)
Complexity Analysis
Each node is visited once Each edge is visited twice
Why?
O( |V| + |E|)
![Page 14: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/14.jpg)
Another Example
![Page 15: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/15.jpg)
Another Example
![Page 16: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/16.jpg)
Connectivity in Undirected Graph
If A can reach B Then B can reach A
If A can reach B && B can reach C Then A can reach C
Also C can reach A
Divide V into smaller subset of vertices that can reach each other
![Page 17: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/17.jpg)
Connected Component Problem Input:
A graph Output:
Marking in every vertices identify the connected component Let it be an array ccnum, indexed by
vertices
![Page 18: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/18.jpg)
Solution
Define global variable cc
In previsit() ccnum[v] = cc
Before calling each explore cc++
![Page 19: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/19.jpg)
Ordering in Visit
procedure previsit(v)pre[v] = clockclock = clock + 1
procedure postvisit(v)post[v] = clockclock = clock + 1
![Page 20: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/20.jpg)
Ordering in Visit
•The interval for node u is [pre(u),post(u)]•The inverval for u,v is either• Contained • disjointed• Never intersect
![Page 21: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/21.jpg)
DFS in Directed Graph
![Page 22: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/22.jpg)
Type of Edge in Directed Graph
![Page 23: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/23.jpg)
Directed Acyclic Graph (DAG)
A directed Graph without a cycle Has “source”
A node having only “out” edge Has “sink”
A node having only “in” edge
How can we detect that a graph is a DAG What should be the property of “source” and
“sink” ?
![Page 24: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/24.jpg)
Solution
A directed graph is acyclic if and only if it has no back edge
Sink Having lowest post number
Source Having highest post number
![Page 25: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/25.jpg)
Linearization of Graph
for DAG, we can have an ordering of node
Think of an edge as Causality Time-dependency
AB means A has to be done before B
Linearization ordering of node by causality
![Page 26: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/26.jpg)
Linearization
One possible linearizationB,A,D,C,E,F
Order of work that can be done w/o violating the causality constraints
![Page 27: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/27.jpg)
Topological Sorting Problem
Input: A DAG (non-dag cannot be linearized)
Output: A sequence of vertices
If we have a path from A to B A must come before B in the sequence
![Page 28: Nattee Niparnan. Graph A pair G = (V,E) V = set of vertices (node) E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ((1,2),(2,3),(3,5),(1,4),(4,](https://reader036.vdocuments.net/reader036/viewer/2022062718/56649e7e5503460f94b8269f/html5/thumbnails/28.jpg)
Topological Sorting
Do DFS List node by post number
(descending)
1,12
2,9
3,8 4,5
6,7
10,11