what is a graph ?
DESCRIPTION
What is a graph ?. 1. 2. 3. 5. 4. What is a graph ?. G=(V,E). V = a set of vertices E = a set of edges. edge = unordered pair of vertices. 1. 2. 3. 5. 4. What is a graph ?. G=(V,E). V = {1,2,3,4,5} E = {{1,5}, {3,5}, {2,3}, {2,4}, {3,4}}. 1. 2. 3. 5. 4. What is a graph ?. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/1.jpg)
What is a graph ?
123
45
![Page 2: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/2.jpg)
What is a graph ?
G=(V,E)
V = a set of verticesE = a set of edges
edge = unordered pair of vertices
123
45
![Page 3: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/3.jpg)
What is a graph ?
G=(V,E)
V = {1,2,3,4,5}E = {{1,5}, {3,5}, {2,3}, {2,4}, {3,4}}
123
45
![Page 4: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/4.jpg)
What is a graph ?
G=(V,E)
V = {1,2,3,4,5}E = {{1,5}, {2,3}, {2,4}, {3,4}}
123
45
![Page 5: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/5.jpg)
Connectedness
1 23
45
1 23
45
connected
not connected
How can we check if a graph is connected?
![Page 6: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/6.jpg)
Representing a graph
|V| * |V| symmetric matrix A
with Ai,j = 1 if {i,j} E Ai,j = 0 otherwise
adjacency matrix
![Page 7: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/7.jpg)
Representing a graph
adjacency matrix1 2
3
450 0 1 0 1
0 0 1 1 0
1 1 0 1 0
0 1 1 0 0
1 0 0 0 0
space = (V2)
![Page 8: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/8.jpg)
Representing a graph
adjacency matrix1 2
3
450 0 1 0 1
0 0 1 1 0
1 1 0 1 0
0 1 1 0 0
1 0 0 0 0 space = (V2)
is {u,v} an edge ? (?)list neighbors of v (?)
![Page 9: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/9.jpg)
Representing a graph
adjacency matrix1 2
3
450 0 1 0 1
0 0 1 1 0
1 1 0 1 0
0 1 1 0 0
1 0 0 0 0 space = (V2)
is {u,v} an edge ? (1)list neighbors of v (n)
![Page 10: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/10.jpg)
Representing a graph
adjacency lists
for each vertex v Vlinked list of neighbors of v
![Page 11: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/11.jpg)
Representing a graph
adjacency lists1 2
3
45
space = (E)
1: 3,52: 3,43: 1,2,44: 2,35: 1
![Page 12: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/12.jpg)
Representing a graph
adjacency lists1 2
3
45
space = (E)
1: 3,52: 3,43: 1,2,44: 2,35: 1
is {u,v} an edge ? (?)list neighbors of v (?)
![Page 13: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/13.jpg)
Representing a graph
adjacency lists1 2
3
45
space = (E)
1: 3,52: 3,43: 1,2,44: 2,35: 1
is {u,v} an edge ? (min(dv,du))list neighbors of v (dv)
![Page 14: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/14.jpg)
Representing a graph
adjacency listsspace = (E)1: 3,5
2: 3,43: 1,2,44: 2,35: 1
0 0 1 0 1
0 0 1 1 0
1 1 0 1 0
0 1 1 0 0
1 0 0 0 0
space = (V2)adjacency matrix
is {u,v} in E ? (min{du,dv})neigbors of v ? (dv)
is {u,v} in E ? (1)neigbors of v ? (n)
![Page 15: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/15.jpg)
Counting connected components
How can we check if a graph is connected?
INPUT: graph G given by adjacency list
OUTPUT: number of components of G
![Page 16: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/16.jpg)
BFS (G,v)
seen[v] true enqueue(Q,v) while Q not empty do w dequeue(Q) for each neighbor u of w if not seen[u] then seen[u] true enqueue(Q,u)
G – undirected graph, V={1,...,n}seen[v] = false for all v VQ=queue (FIFO)
![Page 17: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/17.jpg)
Counting connected components
C 0 for all v V do seen[v] false for all v V do if not seen[v] then C++ BFS(G,v)
output G has C connected components
![Page 18: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/18.jpg)
DFS
explore(G,v) visited[v] true for each neighbor u of v if not visited(u) then explore(G,u)
G – undirected graph, V={1,...,n}visited[v] = false for all v V
![Page 19: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/19.jpg)
DFS
explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++
G – undirected graph, V={1,...,n}visited[v] = false for all v V
![Page 20: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/20.jpg)
DFS explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++
vertex Iv := [pre[v],post[v]]
“interval property” for u,v V either * Iv and Iu are disjoint, or * one is contained in the other
![Page 21: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/21.jpg)
DFS explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++A
B
C D
![Page 22: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/22.jpg)
DFS explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++A
B
C D
tree edges
![Page 23: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/23.jpg)
Digraphs (directed graphs)
G=(V,E)
V = a set of verticesE = a set of edges
edge = ordered pair of vertices
uv (u,v)
![Page 24: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/24.jpg)
Digraphs (directed graphs)
adjacency listsfor each vertex v Vlinked list of out-neighbors of v
|V| * |V| matrix A
with Ai,j = 1 if (i,j) E Ai,j = 0 otherwise
adjacency matrix
![Page 25: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/25.jpg)
a path = sequence of vertices v1,v2,...,vk such that (v1,v2) E, ... , (vk-1,vk) E
Digraphs (directed graphs)
![Page 26: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/26.jpg)
DAGs (acyclic digraphs)
a cycle = sequence of vertices v1,v2,...,vk such that (v1,v2) E, ... , (vk-1,vk),(vk,v1) E
DAG = digraph with no cycle
![Page 27: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/27.jpg)
Topological sort (linearization)
INPUT: DAG G given by adjacency list
OUTPUT: ordering of vertices such that edges go forward
![Page 28: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/28.jpg)
DFS on digraphs
explore(G,v) visited[v] true pre[v] clock; clock++ for each out-neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++
G = digraph, V={1,...,n}visited[v] = false for all v V
![Page 29: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/29.jpg)
DFS on digraphs
A
B
C D
![Page 30: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/30.jpg)
DFS on digraphs
A
B
C D
root
descendant, ancestorchild, parent
![Page 31: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/31.jpg)
DFS on digraphs
A
B
C D
tree edge
![Page 32: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/32.jpg)
DFS on digraphs
A
B
C D
tree edge
![Page 33: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/33.jpg)
DFS on digraphs
A
B
C D
tree edgeback edge
![Page 34: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/34.jpg)
DFS on digraphs
A
B
C D
tree edgeback edge
cross edge
![Page 35: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/35.jpg)
DFS on digraphs
A
B
C D
tree edgeback edge
cross edge
forward edge
![Page 36: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/36.jpg)
Relationships between the intervals?
A
B
C D
tree edgeback edge
cross edge
forward edge
![Page 37: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/37.jpg)
Topological sort using DFS
Lemma: digraph is a DAG if and only if DFS has a back edge.
![Page 38: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/38.jpg)
Topological sort using DFSLemma: digraph is a DAG if and only if DFS has a back edge.
explore(G,v) visited[v] true pre[v] clock; clock++ for each neighbor u of v if not visited(u) then explore(G,u) post[v] clock; clock++
Lemma: in a DAG every edge goes to a vertex with lower post
![Page 39: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/39.jpg)
(strong) connectedness
a digraph G is strongly connectedif for every u,v V there exists a path from u to v in G
![Page 40: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/40.jpg)
(strong) connectedness
How to check if a digraph isstrongly connected?
![Page 41: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/41.jpg)
(strong) connectedness
How to check if a digraph isstrongly connected?
for every uV do DFS(G,u) check if every vV was visited
![Page 42: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/42.jpg)
(strong) connectedness
How to check if a digraph isstrongly connected?
pick some uV DFS(G,u)check if every vV was visitedDFS(reverse(G),u)check if every vV was visited
![Page 43: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/43.jpg)
Strongly connected components
DAG of strongly connected components
![Page 44: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/44.jpg)
Strongly connected components
Lemma: G and reverse(G) havethe same strongly connectedcomponents.
![Page 45: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/45.jpg)
Strongly connected components
DAG of strongly connected components
![Page 46: What is a graph ?](https://reader036.vdocuments.net/reader036/viewer/2022062304/56813bae550346895da4e313/html5/thumbnails/46.jpg)
Strongly connected components
for all v V do color[v] white
for all v V do if color[v]=white then DFS(reverse(G),v) DFS(G,u) (vertices in order post[])