0% found this document useful (0 votes)
7 views

DS Graphs

The document discusses different types of graphs including directed and undirected graphs. It defines key graph terminology like vertices, edges, degree of a vertex, adjacency matrix/list representation. It also explains graph concepts like connected components, trees, spanning trees, breadth-first search and depth-first search algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DS Graphs

The document discusses different types of graphs including directed and undirected graphs. It defines key graph terminology like vertices, edges, degree of a vertex, adjacency matrix/list representation. It also explains graph concepts like connected components, trees, spanning trees, breadth-first search and depth-first search algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 88

Graphs-1

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}

E= {(a,b),(a,c),(a,d), (b,e), (c,d),


(c,e), (d,e)}
c

d e
Directed v/s Undirected graphs

(a)An undirected graph is one in which the pair of vertices in a edge is


unordered, (v0, v1) = (v1,v0)
(b)A directed graph is one in which each edge is a directed pair of
vertices, <v0, v1> ≠ <v1,v0>
Graph Terminology
Graph Terminology …
Graph Terminology…
Directed Graphs
Directed Graph…
Directed Graph…
Directed Graph…
Graph Representation

An undirected graph and its adjacency matrix representation.

An undirected graph and its adjacency list representation.


Definitions
An undirected graph is connected if every pair of
vertices is connected by a path.
A forest is an acyclic graph, and a tree is a connected
acyclic graph.
A graph that has weights associated with each edge is
called a weighted graph.
adjacent vertices: connected by an edge
degree (of a vertex): # of adjacent vertices.
path: sequence of vertices v1,v2,. . .vk such that
consecutive vertices vi and vi+1 are adjacent.
Connected graph
connected graph: any two vertices are connected by
some path

Connected not connected


Connected Components
• The connected components of an undirected graph
are the equivalence classes of vertices under the “is
reachable from” relation.

• A graph with three connected components: {1, 2,


3,4}; {5, 6, 7}; and {8, 9}.
Trees and Forests
A tree is an undirected graph T such that
T is connected
T has no cycles
This definition of tree is different
from the one of a rooted tree
Tree

A forest is an undirected graph without cycles


The connected components of a forest are trees

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

n5
m  (5    
Connectivity
n = #vertices
m = #edges
For a tree m = n - 1 n5
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.

DFS-Visit is called once for each white vertex vV


when it’s painted gray the first time.
Lines 4-7 of DFS-Visit is executed |Adj[v]| times. The
total cost of executing DFS-Visit is vV|Adj[v]| = (E)

Total running time of DFS is (V+E).


DFS on directed Graph
Four type of edges are produces
1. Tree edges: are edges (u,v) if v was first discovered
by exploring edge (u,v).
2. Back edges: are edges (u,v) connecting a vertex u
to an ancestor v in DFS tree. Self loops are also
called back edges.
3. Forward edges: are non-tree edges (u,v)
connecting a vertex u to a descendent v in DFS tree.
4. Cross edges: are all other edges. Can go between
vertices in the same DFS tree or they can go
between vertices in different DFS trees.
DFS on directed Graph

u v w
1/

x y z
Example (DFS)

u v w
1/ 2/

x y z

Consider edge (u,v)


Example (DFS)

u v w
1/ 2/

3/
x y z

From v, Consider edge (v, y)


Example (DFS)

u v w
1/ 2/

4/ 3/
x y z

From y, Consider edge (y, x)


Example (DFS)

u v w
1/ 2/

4/ 3/
x y z

From x, Consider edge (x, v) :


do not include in tree
Example (DFS)

u v w
1/ 2/

4/5 3/
x y z

Vertex x, no more edges, finish it.


Example (DFS)

u v w
1/ 2/

4/5 3/6
x y z

From y, no more edges, finish it


Example (DFS)

u v w
1/ 2/7

4/5 3/6
x y z

From v, no more edges, finish it


Example (DFS)

u v w
1/ 2/7

F B

4/5 3/6
x y z

From u, consider edge (u,x),


do not include
Example (DFS)

u v w
1/8 2/7

F B

4/5 3/6
x y z

From u, no more edges, finish it


Example (DFS)

u v w
1/8 2/7 9/

F B

4/5 3/6
x y z

DFS from u ends, start again from w


Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6
x y z

From w ends, Consider (w,y) again


from w
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/


x y z
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/ B


x y z
Example (DFS)

u v w
1/8 2/7 9/

F B C

4/5 3/6 10/11 B


x y z
Example (DFS)

u v w
1/8 2/7 9/12

F B C

4/5 3/6 10/11 B


x y z
Classification of edges in DFS tree
Each edge (u,v) can be classified by the color of the
vertex v that is reached when edge is first explored.
1. WHITE indicates a tree edge.
2. GRAY indicates a back edge.
3. BLACK indicates a forward edge or cross edge.
In case 3, if d[u] < d[v] : it is a forward edge.
In case 3, if d[u] > d[v]: it is a cross edge
DFS :Applications
Path Finding:
We can specialize the DFS algorithm to find a path
between two given vertices u and z using the
template method pattern
We call DFS(G, u) with u as the start vertex
We use a stack S to keep track of the path between
the start vertex and the current vertex
As soon as destination vertex z is encountered, we
return the path as the contents of the stack
Path Finding:
Algorithm pathDFS(G, v, z) Algorithm pathDFS-VISIT(G, v, z)
1. for each vertex u ϵ V[G] 1. Color[v] GRAY
2. do color[u] WHITE 2. S.push(v)
3. Done=FALSE 3. If (v  z)
4. pathDFS-VISIT(G,v,z) 4. THEN Done=TRUE
5. return S.elements
6. for each u  Adj[v]
7. do if (color[u] = WHITE)
8. THEN pathDFS(G,u,z)
9. if (Done) THEN return;
10. S.pop()
11. Color[v] BLACK
Minimum Spanning Trees
Spanning Trees
• A spanning tree of a graph is a tree and is a subgraph
that contains all the vertices.
• A graph may have many spanning trees; for example,
the complete graph on four vertices has sixteen
spanning trees:
Spanning trees
Minimum Spanning Trees (MSTs)
Suppose that the edges of the graph have weights or
lengths. The weight of a tree will be the sum of
weights of its edges.
Based on the example, we can see that different trees
have different lengths.
The question is: how to find the minimum length
spanning tree?
The question can be solved by many different
algorithms, here is two classical minimum-spanning
tree algorithms :
Kruskal's Algorithm
Prim's Algorithm
Minimum Spanning Tree

An undirected graph and its minimum spanning tree


MST: Problem
• Undirected, connected graph G = (V,E)
• Weight function W: E R (assigning cost or length
or other values to edges)
• Cost/weigth of MST: sum of weights of all edges in
MST.
• Problem is to find a Minimum spanning tree: tree that
connects all the vertices and having minimum weight.
w(T )  w(u,v)
(u,v)T
Generic MST Algorithm
Generic-MST(G, w)
1 A // Contains edges that belong to a MST
2while A does not form a spanning tree do
3 Find an edge (u,v) that is safe for A
4 AA{(u,v)}
5 return A

Safe edge – edge that does not destroy A’s property


The algorithm manages a set of edges A maintaining the followingloop
invariant
• Prior to each iteration, A is a subset of some minimum spanningtree.
• At each step, an edge is determined that can be added to Awithout
violating this invariant. Such an edge is called a Safe Edge.
Kruskal’s Algorithm
Create a forest of trees from the vertices
Repeatedly merge trees by adding “safe edges”
until only one tree remains
A “safe edge” is an edge of minimum weight
which does not create a cycle
Kruskal's Algorithm
Edge based algorithm
Add the edges one at a time, in increasing weight
order
The algorithm maintains A – a forest of trees. An
edge is accepted it if connects vertices of distinct
trees
We need a data structure that maintains a partition,
i.e.,a collection of disjoint sets
Make-Set(v): S  {v}
Union(Si,Sj): S  S – {Si,Sj}  {Si  Sj}
FindSet(S, x): returns unique Si  S, where x  Si
Kruskal's Algorithm
The algorithm adds the cheapest edge that connects
two trees of the forest
MST-Kruskal(G,w)
1 A    set of edges forming MST
2 for each vertex v  V[G] do
3 Make-Set(v)
4 sort the edges of E by non-decreasing weight w
5 for each edge (u,v)  E, in order by non-
decreasing weight do
6 if Find-Set(u)  Find-Set(v) then
7 A  A  {(u,v)}
8 Union(u,v)  Union of sets containing u and v
9 return A
Kruskal’s algorithm: example
Kruskal’s algorithm: example…
Kruskal’s algorithm: example…
Kruskal’s algorithm: example…
Kruskal’s Algorithm: Running Time
Initialization O(V) time
Sorting the edges (E lg E) = (E lg V) (why?)
O(E) calls to FindSet
Union costs
Let t(v) – the number of times v is moved to a new
cluster
Each time a vertex is moved to a new cluster the size of
the cluster containing the vertex at least doubles: t(v) 
log V
Total time spent doing Union  t(v)  V log V
vV
Total time: O(E lg V)
Prim’s Algorithm
Vertex based algorithm
It is a greedy algorithm.
Start by selecting an arbitrary vertex, include it
into the current MST.
Grow the current MST by inserting into it the
vertex closest to one of the vertices already in
current MST.
Grows one tree T, one vertex at a time
A cloud covering the portion of T already computed
Label the vertices v outside the cloud with key[v] –
the minimum weigth of an edge connecting v to a
vertex in the cloud, key[v] = , if no such edge exists
Prim’s Algorithm
MST-Prim(G,w,r)
01 Q  V[G] // Q a priority queue – vertices out of T
02 for each u  Q
3. key[u]  
4. key[r]  0
5. [r]  NIL
6. while Q   do
07 u  ExtractMin(Q) // making u part of T
08 for each v  Adj[u] do
09 if v  Q and w(u,v) < key[v] then
10 [v]  u
11 key[v]  w(u,v)
Prim’s Algorithm: example
∞ ∞ ∞ 4 ∞ ∞

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.

• A topological sort of a graph can be viewed as an


ordering of its vertices along a horizontal line so that
all directed edges go from left to right.
Topological Sort:Example

Undershorts
Socks
Watch
Pants Shoes
Shirt

Belt Tie

Jacket

Socks Undershorts Pants Shoes Watch Shirt Belt Tie Jacket


Topological sort
• There are often many possible topological sorts of a
given DAG (Directed Acyclic Graph)
• Topological orders for this DAG :
1 2
 1,2,5,4,3,6,7
 2,1,5,4,7,3,6
3 4 5
 2,5,1,4,7,3,6
 Etc. 6 7

• Each topological order is a feasible schedule.


Topological Sorts for Cyclic Graphs?

1 2
Impossible!
3

If v and w are two vertices on a cycle, there


exist paths from v to w and from w to v.
Any ordering will contradict one of these
paths
Topological Sort: Algorithm
TOPOLOGICAL-SORT(G)
1. Call DFS(G) to compute finishing time f[v] for each vertex v.
2. As each vertex is finished, insert it onto the front of a linked list.
3. Return the linked list of vertices.
Topological Sort
11/16
17/18
Undershorts
Socks

Watch 9/10
12/15 Pants Shoes
Shirt 13/14
1/8

6/7 Belt Tie 2/5

Jacket 3/4

Socks Undershorts Pants Shoes Watch Shirt Belt Tie Jacket


17/18 11/16 12/15 13/14 9/10 1/8 6/7 2/5 3/4

All edges of G are going from left to right only


THANKS

You might also like