Download presentation
Presentation is loading. Please wait.
Published byBrycen Life Modified over 10 years ago
1
What is a graph ? 1 2 3 4 5
2
G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices 1 2 3 4 5
3
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 4 5
4
What is a graph ? G=(V,E) V = {1,2,3,4,5} E = {{1,5}, {2,3}, {2,4}, {3,4}} 1 2 3 4 5
5
Connectedness 1 2 3 4 5 1 2 3 4 5 connected not connected How can we check if a graph is connected?
6
Representing a graph |V| * |V| symmetric matrix A with A i,j = 1 if {i,j} E A i,j = 0 otherwise adjacency matrix
7
Representing a graph adjacency matrix 1 2 3 4 5 00101 00110 11010 01100 10000 space = (V 2 )
8
Representing a graph adjacency matrix 1 2 3 4 5 00101 00110 11010 01100 10000 space = (V 2 ) is {u,v} an edge ? (?) list neighbors of v (?)
9
Representing a graph adjacency matrix 1 2 3 4 5 00101 00110 11010 01100 10000 space = (V 2 ) is {u,v} an edge ? (1) list neighbors of v (n)
10
Representing a graph adjacency lists for each vertex v V linked list of neighbors of v
11
Representing a graph adjacency lists 1 2 3 4 5 space = (E) 1: 3,5 2: 3,4 3: 1,2,4 4: 2,3 5: 1
12
Representing a graph adjacency lists 1 2 3 4 5 space = (E) 1: 3,5 2: 3,4 3: 1,2,4 4: 2,3 5: 1 is {u,v} an edge ? (?) list neighbors of v (?)
13
Representing a graph adjacency lists 1 2 3 4 5 space = (E) 1: 3,5 2: 3,4 3: 1,2,4 4: 2,3 5: 1 is {u,v} an edge ? (min(d v,d u )) list neighbors of v (d v )
14
Representing a graph adjacency lists space = (E) 1: 3,5 2: 3,4 3: 1,2,4 4: 2,3 5: 1 00101 00110 11010 01100 10000 space = (V 2 ) adjacency matrix is {u,v} in E ? (min{d u,d v }) neigbors of v ? (d v ) is {u,v} in E ? (1) neigbors of v ? (n)
15
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
16
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 V Q=queue (FIFO)
17
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
18
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
19
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
20
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 I v := [pre[v],post[v]] interval property for u,v V either * I v and I u are disjoint, or * one is contained in the other
21
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
22
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
23
Digraphs (directed graphs) G=(V,E) V = a set of vertices E = a set of edges edge = ordered pair of vertices u v (u,v)
24
Digraphs (directed graphs) adjacency lists for each vertex v V linked list of out-neighbors of v |V| * |V| matrix A with A i,j = 1 if (i,j) E A i,j = 0 otherwise adjacency matrix
25
a path = sequence of vertices v 1,v 2,...,v k such that (v 1,v 2 ) E,..., (v k-1,v k ) E Digraphs (directed graphs)
26
DAGs (acyclic digraphs) a cycle = sequence of vertices v 1,v 2,...,v k such that (v 1,v 2 ) E,..., (v k-1,v k ),(v k,v 1 ) E DAG = digraph with no cycle
27
Topological sort (linearization) INPUT: DAG G given by adjacency list OUTPUT: ordering of vertices such that edges go forward
28
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
29
DFS on digraphs A B C D
30
A B C D root descendant, ancestor child, parent
31
DFS on digraphs A B C D tree edge
32
DFS on digraphs A B C D tree edge
33
DFS on digraphs A B C D tree edge back edge
34
DFS on digraphs A B C D tree edge back edge cross edge
35
DFS on digraphs A B C D tree edge back edge cross edge forward edge
36
Relationships between the intervals? A B C D tree edge back edge cross edge forward edge
37
Topological sort using DFS Lemma: digraph is a DAG if and only if DFS has a back edge.
38
Topological sort using DFS Lemma: 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
39
(strong) connectedness a digraph G is strongly connected if for every u,v V there exists a path from u to v in G
40
(strong) connectedness How to check if a digraph is strongly connected?
41
(strong) connectedness How to check if a digraph is strongly connected? for every u V do DFS(G,u) check if every v V was visited
42
(strong) connectedness How to check if a digraph is strongly connected? pick some u V DFS(G,u) check if every v V was visited DFS(reverse(G),u) check if every v V was visited
43
Strongly connected components DAG of strongly connected components
44
Strongly connected components Lemma: G and reverse(G) have the same strongly connected components.
45
Strongly connected components DAG of strongly connected components
46
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[])
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.