What is a graph ?
123
45
What is a graph ?
G=(V,E)
V = a set of verticesE = a set of edges
edge = unordered pair of vertices
123
45
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
What is a graph ?
G=(V,E)
V = {1,2,3,4,5}E = {{1,5}, {2,3}, {2,4}, {3,4}}
123
45
Connectedness
1 23
45
1 23
45
connected
not connected
How can we check if a graph is connected?
Representing a graph
|V| * |V| symmetric matrix A
with Ai,j = 1 if {i,j} E Ai,j = 0 otherwise
adjacency matrix
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)
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 (?)
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)
Representing a graph
adjacency lists
for each vertex v Vlinked list of neighbors of v
Representing a graph
adjacency lists1 2
3
45
space = (E)
1: 3,52: 3,43: 1,2,44: 2,35: 1
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 (?)
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)
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)
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
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)
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
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
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
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
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
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
Digraphs (directed graphs)
G=(V,E)
V = a set of verticesE = a set of edges
edge = ordered pair of vertices
uv (u,v)
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
a path = sequence of vertices v1,v2,...,vk such that (v1,v2) E, ... , (vk-1,vk) E
Digraphs (directed graphs)
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
Topological sort (linearization)
INPUT: DAG G given by adjacency list
OUTPUT: ordering of vertices such that edges go forward
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
DFS on digraphs
A
B
C D
DFS on digraphs
A
B
C D
root
descendant, ancestorchild, parent
DFS on digraphs
A
B
C D
tree edge
DFS on digraphs
A
B
C D
tree edge
DFS on digraphs
A
B
C D
tree edgeback edge
DFS on digraphs
A
B
C D
tree edgeback edge
cross edge
DFS on digraphs
A
B
C D
tree edgeback edge
cross edge
forward edge
Relationships between the intervals?
A
B
C D
tree edgeback edge
cross edge
forward edge
Topological sort using DFS
Lemma: digraph is a DAG if and only if DFS has a back edge.
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
(strong) connectedness
a digraph G is strongly connectedif for every u,v V there exists a path from u to v in G
(strong) connectedness
How to check if a digraph isstrongly connected?
(strong) connectedness
How to check if a digraph isstrongly connected?
for every uV do DFS(G,u) check if every vV was visited
(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
Strongly connected components
DAG of strongly connected components
Strongly connected components
Lemma: G and reverse(G) havethe same strongly connectedcomponents.
Strongly connected components
DAG of strongly connected components
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[])