DS Graphs
DS Graphs
Data Structure
Graphs
Divyashikha Sethia
[email protected]
What is a Graph?
A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (u,v) is a pair of vertices
Example:
a b V= {a,b,c,d,e}
d e
Directed v/s Undirected graphs
Forest
Spanning Trees and Forests
A spanning tree of a
connected graph is a
spanning subgraph that is a
tree
A spanning tree is not
unique unless the graph is a Graph
tree
Spanning trees have
applications to the design of
communication networks
A spanning forest of a graph
is a spanning subgraph that
is a forest Spanning tree
Connectivity
Let n = #vertices, and m = #edges
A complete graph: one in which all pairs of vertices are
adjacent
How many total edges in a complete graph?
Each of the n vertices is incident to n-1 edges, however, we would have
counted each edge twice! Therefore, intuitively, m = n(n -1)/2.
Therefore, if a graph is not complete, m < n(n -1)/2
n5
m (5
Connectivity
n = #vertices
m = #edges
For a tree m = n - 1 n5
m 4
If m < n - 1, G is not
connected
n 5
m 3
Degree of vertex
Undirected Graph: 0 2
Degree of vertex
3 1 2 3
3G
2
0 in:1, out: 1
Directed Graph:
in-degree and out-degree 1 in: 1, out: 2
2 in: 1, out: 0
Breadth First Search (BFS)
Input: Graph G = (V, E), either directed or undirected,
and source vertex s V.
Output:
d[v] = distance (smallest # of edges, or shortest path) from s to v,
for all v V. d[v] = if v is not reachable from s.
[v] = u such that (u, v) is last edge on shortest path s v.
u is v’s predecessor.
Builds breadth-first tree with root s that contains all reachable
vertices.
BFS: some points
A vertex is “discovered” the first time it is encountered duringthe
search.
A vertex is “finished” if all vertices adjacent to it havebeen
discovered.
Color the vertices to keep track of progress.
White – Undiscovered.
Gray – Discovered but not finished.
Black – Finished.
Colors are required only to reason about the algorithm. Can be implemented
without colors.
BFS: Algorithm
BFS(G,s)
white: undiscovered
1. for each vertex u in V[G] – {s}
gray: discovered
2 do color[u] white black: finished
3 d[u]
4 [u] nil Q: a queue of discovered
5 color[s] gray vertices
6 d[s] 0 color[v]: color of v
7 [s] nil d[v]: distance from s to v
8 Q [u]: predecessor of v
9 enqueue(Q,s)
10 whileQ
11 do u dequeue(Q)
12 for each v in Adj[u]
13 do if color[v] = white
14 then color[v] gray
15 d[v] d[u] + 1
16 [v] u
17 enqueue(Q,v)
18 color[u] black
BFS: Example
r s t u
0
v w x y
Q: s
0
BFS: Example
r s t u
1 0
1
v w x y
Q: w r
1 1
BFS: Example
r s t u
1 0 2
1 2
v w x y
Q: r t x
12 2
BFS: Example
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
2 2 2
BFS: Example
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
22 3
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
23 3
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
3 3
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
3
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q:
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
BF Tree
BFS: Analysis
Initialization takes O(V).
Traversal Loop
After initialization, each vertex is enqueued and dequeued at most
once, and each operation takes O(1). So, total time for queuing is
O(V).
The adjacency list of each vertex is scanned at most once. The
sum of lengths of all adjacency lists is (E).
Summing up over all vertices => total running time of
BFS is O(V+E), linear in the size of the adjacency list
representation of graph.
Depth First Search traversal
• Input: G = (V, E), directed or undirected. No source
vertex given!
• Output:
▫ 2 timestamps on each vertex. Integers between 1 and 2|V|.
d[v] = discovery time (v turns from white to gray)
f [v] = finishing time (v turns from gray to black)
▫ [v] : predecessor of v = u, such that v was discovered during the
scan of u’s adjacency list.
• Uses the same coloring scheme for vertices as BFS.
DFS: Algorithm
DFS(G) DFS-Visit(u)
1. for each vertex u V[G] 1. color[u] GRAY // White vertex
2. do color[u] WHITE u has been discovered
3. [u] NIL 2. time time + 1
4. time 0 3. d[u] time
5. for each vertex u V[G] 4. for each v Adj[u]
6. do if color[u] = WHITE 5. do if color[v] = WHITE
7. then DFS-Visit(u) 6. then [v] u
7. DFS-Visit(v)
8. color[u] BLACK // Blacken u;
Uses a global timestamp time. it is finished.
9. f[u] time time + 1
DFS: Example
A unexplored vertex 1/ A
A discovered vertex
2/ B D E
A finished vertex
unexplored edge
C
discovery edge
back edge Back
edge
1/ A 1/ A
B D E 2/ B D E
C tree C
edge 3/
Example…
1/ 1/
A A
B D E 2/ B D 4/5 E
2/
C C
3/
3/
1/ A
B D 4/5 E
2/
6/
C
3/
Example…
1/10 A
(u,v) is Back edge if
d(v) < d(u)
B D 4/5 E
2/9
6/7 (u,v) is tree edge if
vertex v is discovered first from
C
vertex u.
3/8
Properties of DFS
Property 1
DFS-VISIT(G, u) visits all the vertices and edges in the connected
component of v.
Property 2
The discovery edges labeled by DFS-VISIT(G, v) form a spanning tree of
the connected component of v.
Property 3
The DFS(G) form a forest of spanning trees of the connected components
of G.
B D E
C
Analysis of DFS
Loops on lines 1-2 & 5-7 take (V) time, excluding
time to execute DFS-Visit.
u v w
1/
x y z
Example (DFS)
u v w
1/ 2/
x y z
u v w
1/ 2/
3/
x y z
u v w
1/ 2/
4/ 3/
x y z
u v w
1/ 2/
4/ 3/
x y z
u v w
1/ 2/
4/5 3/
x y z
u v w
1/ 2/
4/5 3/6
x y z
u v w
1/ 2/7
4/5 3/6
x y z
u v w
1/ 2/7
F B
4/5 3/6
x y z
u v w
1/8 2/7
F B
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B C
4/5 3/6
x y z
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/
F B C
u v w
1/8 2/7 9/12
F B C
0 ∞ ∞ 0 ∞ ∞
∞ ∞ ∞ 8 ∞ ∞
4 8 ∞ 4 8 7
∞ ∞ 0 2 ∞
0
8 ∞ ∞ 7 6 4
Prim’s Algorithm: example…
4 8 7 4 8 7
0 2 10 0 2 10
7 2 4 1 2 4
4 8 7 4 8 7
0 2 10 0 2 9
1 2 4 1 2 4
Prim’s Algorithm: example…
4 8 7
2 9
0
1 2 4
Directed Acyclic Graph (DAG)
A Directed Graph without a cycle.
Undershorts
Socks
Watch
Pants Shoes
Shirt
a DAG implies an
Belt Tie ordering on events
In a complex DAG, it
can be hard to find a
Jacket schedule that obeys
all the constraints.
2
Topological Sort
• For a directed acyclic graph G = (V,E), a topological
sort is a linear ordering of all vertices of G such that
if G contains an edge (u,v), then u appears before v in
the ordering.
Undershorts
Socks
Watch
Pants Shoes
Shirt
Belt Tie
Jacket
1 2
Impossible!
3
Watch 9/10
12/15 Pants Shoes
Shirt 13/14
1/8
Jacket 3/4