Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

unit4(DS

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 98

UNIT-IV

GRAPH
Graph
A graph G=(V,E) consists of a set of vertices,V and a
set of edges,E.Each edge is a pair(v,w) where v,w ɛ
V.Edges are referred to as arcs.
P Q
R

S T
Vertices={P,Q,R,S,T}
Edges={PQ,PS,PT,QT,QS,QR,RS,ST}
directed graph
Directed graph or digraph
A directed graph G(V,E) contains a non empty
set of vertices V and a set of directed edges E.
Edges of a directed graph has a specific
direction
Comparison
graph tree
1. Graph is a non linear data Tree is a non linear data
structure structure

2. It is a collection of vertices It is a collection of nodes


and edges and links

3. Each node has any number In binary tree every node


of edges has atmost 2 children
4. A cycle can be formed No cycle can be formed
5. There is no unique root There is a unique node
node called root

6. Application: finding Application: expression


shortest path tree,game tree
Parallel Edges
• If a graph G contains pairs of nodes to be connected by
several distinct edges ,they are called parallel edges of G.
• It is denoted by {u,v}1,{u,v}2…….{u,v}k
• Where k represents the number of distinct edges between u
and v v1 v2
e1
e5 e4 e2
v3 v4
Self loops
Edges of the form {u,u} (ie) with the two end points being the
same are known as self loops.
A B

C D

Simple graph
A graph that has neither self-loop nor parallel edges is called as
simple graph
Complete graph
A complete graph is a special type of simple
graph with n vertices in which there is an edge
between every pair of distinct vertices.
Eg
v1 v2

v4 v3
Degree or valency
The number of edges incident on a vertex
vi,with self-loop counted twice is called the
degree d(vi) of vertex vi.
eg
d(v1)=d(v3)=3
d(v2)=d(v4)=2
The sum of the degrees of all vertices in G is
twice the number of edges in G (ie)
Cycle graph
A graph which consist of a single cycle is known
as cycle graph.

Path
A path in a graph is a sequence of vertices
w1,w2,w3….wn such that (wi,wi+1)ɛE for 1≤i˂N
Connected graph
• An undirected graph is said to be connected if
for every pair of distinct vertices Vi and Vj
inV(G)there is an edge Vi to Vj in G.
Weighted graph
A Weighted graph is a graph which consist of
weights along its edges.
4 1
5
Components
The maximal connected subgraph of a graph is
called components of a graph.
In degree and out degree
In degree is the number of edges that incident
to that vertex
Out degree is the number of edges that are
going away from the vertex.
a b

c d
Representation of graph
Adjacency matrix 1 2 3 4 5
Adjacency list 1 0 1 1 0 0
Adjacency matrix 2 1 0 0 1 0
3 1 0 0 1 1
v1

v3
4 0 1 1 0 1
v2
5 0 0 1 1 0
v4 v5
Adjacency list
a b e N
a
e
b b
a c N
C
c d
b d N
D

c e N
E

a d N
GRAPH TRAVERSAL
• Breadth first traversal
• Depth first traversal
Breadth first traversal
Algorithm
1. Create a graph.
2. Read the vertex from which we want to traverse the
graph say vi.
3. Initialize the visited array to 1 at the index of Vi.
4. Insert the visited vertex vi in the queue
5. Visit the vertex which is at the front of the queue . Delete
it from the queue and place its adjacent nodes in the queue.
6. Repeat step 5 till the queue is not empty.
7. stop
v1
v2

v3 v4 v5

v6 v7
visited queue
1
v1 v1 print v1

v1
visited queue
1
v1 v2 v4 print v2
1
v2
v3
v4
v1,v2
visited queue
1
v1 v4 v5 print v4
1
v2
v3
1
v4
v5
v6 v1,v2,v4
visited queue
1
v1 v5 v3 v6 print v5
1
v2
v3
1
v4 1
v5
v6
v7 v1,v2,v4,v5
visited queue
1
v1 v3 v6 v7 print v3
1
v2
1
v3
1
v4 1
v5
v6
v7 v1,v2,v4,v5,v3
visited queue
1
v1 v6 v7 print v3
1
v2
1
v3
1
v4 1
v5
v6
v7 v1,v2,v4,v5,v3
visited queue
1
v1 v7 print v6
1
v2
1
v3
1
v4 1
v5 1
v6
v7 v1,v2,v4,v5,v3,v6
visited queue
1
v1 print v7
1
v2
1
v3
1
v4 1
v5 1
v6 1
v7 v1,v2,v4,v5,v3,v6,v7
Depth First Traversal(Depth Firth Search)
1. Starts with vertex ‘v’ and making it as visited.
2. Next an unvisited vertex ‘w’ adjacent to v is
selected.
3. Call the same algorithm with vertex ‘w’`
4. Continue the process until all vertices have
been visited.
Algorithm DFS(v)
{
Visited(v)=true
For each vertex ‘w’ adjacent to ‘v’
If(visited(w)==true) then
DFS(w)
}
1

2 3

4 5 6 7

8
visited stack
1
1 1 print 1
2
3
4
5
6
7
8
visited stack
1
1 3 2 print 2
1
2
3
4
5 1,2
6
7
8
visited stack
1
1 3 5 4 print 4
1
2
3
1
4
5 1,2,4
6
7
8
visited stack
1
1 3 5 8 print 8
1
2
3
1
4
5 1,2,4,8
6
7 1
8
visited stack
1
1 3 5 7 6 print 6
1
2
3
1
4
5 1 1,2,4,8,6
6
7 1
8
visited stack
1
1 3 5 7 print 7
1
2
3
1
4
5 1 1,2,4,8,6,7
6 1
7 1
8
visited stack
1
1 3 5 print 5
1
2
3
1
4 1
5 1 1,2,4,8,6,7,5
6 1
7 1
8
visited stack
1
1 3 print 3
1
2
1
3
1
4 1
5 1 1,2,4,8,6,7,5,3
6 1
7 1
8
visited stack
1
1
1
2
1
3
1
4 1
5 1 1,2,4,8,6,7,5,3
6 1
7 1
8
Depth first traversal
• In depth first traversal, start from one vertex,
and traverse the path as deeply as we can go.
• When there is no vertex further ,we traverse
back and search for unvisited vertex.
DFS Visited
1
1 1

2 3 2
3
4
4

1
print 1

1
DFS Visited
1
1 1

2 3 2 1

3
4
4

3 2
print 2
1,2
DFS Visited
1
1 1

2 3 2 1

3
1
4
4

3 4
print 4
1,2,4
DFS Visited
1
1 1

2 3 2 1

1
3
1
4
4

3
print 3
1,2,4,3
Topological sort
A topological sort is an ordering of vertices in a
directed acyclic graph,such that if there is a
path from vi to vj , then vj appears after vi in
the ordering.
Algorithm
• Find any vertex with no incoming edges.
• Then print this vertex and remove it along
with its edges from the graph.
• Apply the same strategy to the rest of the
graph.
• Example

v1 v2

v5
v3 v4

v6 v7

Remove v1
• Example

v2

v5
v3 v4

v6 v7

print v1
Remove v2
• Example

v5
v3 v4

v6 v7

print v1,v2
Remove v5
• Example

v3 v4

v6 v7

print v1,v2,v5
Remove v4
• Example

v3

v6 v7

print v1,v2,v5,v4
Remove v3
• Example

v6 v7

print v1,v2,v5,v4,v3
Remove v7
• Example

v6

print v1,v2,v5,v4,v3,v7
Remove v6
• Example

print v1,v2,v5,v4,v3,v7,v6
BICONNECTIVITY
A connected undirected graph is biconnected if
there are no vertices whose removal
disconnects the rest of the graph.
A

B D E

C
Articulation points or cut vertex
If a graph is not biconnected ,the vertices whose
removal would disconnects the graph are
known as articulation points.
B A

C D
F

G E
• Depth first search provides an algorithm to find all
articulation points in a connected graph
• Step 1:starting at any vertex ,perform a DFS and number
the nodes as they are visited.
• Step 2:for every vertex v in the DFS spanning tree,
compute the lowest numbered vertex called low(v).
• Step 3: low(v) is the minimum of
 num(v)
 The lowest num(w) among all back edges (v,w)
 The lowest low(w) among all tree edges(v,w)
Step 4: the root is an articulation point if and only if it has more
than one child.
Step 5: any other vertex v is an articulation point if and only if v
has some child w such that low(w)≥num(v)
B A

C D
F

G E
A,1/1

B,2/1

C,3/1

D,4/1

G,7/7

E,5/4

F,6/4
Euler circuit
• A puzzle is to reconstruct the figure uing a
pen,drawing each line exactly once.the pen
may not be lifted from the paper while the
drawing is being performed.Also make the pen
finish at the same point at which it is started.
spanning tree
• A spanning tree of an undirected connected
graph is its connected acyclic subgraph (i.e., a
tree) that contains all the vertices of the graph.
minimum spanning tree
• A minimum spanning tree is its spanning tree of
the smallest weight, where the weight of a tree
is defined as the sum of the weights on all its
edges.
2
2 1 3
4
2
2 1 3
4
kruskal algorithm
• Sort all the edges in non decreasing order
of their weight.
• Pick the smallest edge ,check if it forms a
cycle with the ST formed so far.if cycle is not
formed
,include this edge. Else discard it.
• Repeat step 2 until there are n-1 edges in
the ST.
B 5 D
2 1 3
A 2 C
B
BC=1 2 1 3
D
A
AB= C
2
AC=
2
1

b c 6
3
4 4

5 5
a f d

2
8
6
e
1
bc1 b c 6
3
ef2 4 4

ab3 5 5
d
a f
bf4
2
cf4 8
6
af5
e
df5
ae6
cd6
de8
1
bc1 b c 6
3
ef2 4 4

ab3 5 5
d
a f
bf4
2
cf4 8
6
af5
e
df5
ae6
cd6
de8
1
bc1 b c 6
3
ef2 4 4

ab3 5 5
d
a f
bf4
2
cf4 8
6
af5
e
df5
ae6
cd6
de8
1
bc1 b c 6
3
ef2 4 4

ab3 5 5
d
a f
bf4
2
cf4 8
6
af5
e
df5
ae6
cd6
de8
1
bc1 b c 6
3
ef2 4 4

ab3 5 5
d
a f
bf4
2
cf4 8
6
af5
e
df5
ae6
cd6
de8
1

b c
3
4

5
a f d

e
Example
0
3
6
1 5 1 5 3
3 2 2
6 4
4 6
5
Prim’s Algorithm

• Step 1: Select a starting vertex


• Step 2: repeat step 3 and 4 until there are
fringe vertices
• Step 3: select an edge e connecting the tree
vertex and fringe vertex that has minimum
weight
• Step 4: add the selected edge and the vertex to
the minimum Spanning tree T
• Step 5:exit
Tree vertex:vertex that are part of MST
Fringe vertex: vertex that are not a part of T but
Prim’s Algorithm
1

b c 6
3
4 4

5 5
a f d

2
8
6
e
1

b c 6
3
4 4

5 5
a f d

2
8
6
e

Tree vertices Remaining vertices


a(−, −) b(a, 3) c(−,∞) d(−,∞)
e(a, 6) f(a, 5)
1

b c 6
3
4 4

5 5
a f d

2
8
6
e

Tree vertices Remaining vertices


b(a, 3) c(b, 1) d(−,∞) e(a, 6)
f(b, 4)
1

b c 6
3
4 4

5 5
a f d

2
8
6
e

Tree vertices Remaining vertices


c(b, 1) d(c, 6) e(a, 6) f(b, 4)
1

b c 6
3
4 4

5 5
a f d

2
8
6
e

Tree vertices Remaining vertices


f(b, 4) d(f, 5) e(f, 2)
1

b c 6
3
4 4

5 5
a f d

2
8
6
e
Tree vertices Remaining vertices
e(f, 2) d(f, 5)

d(f, 5)
1

b c
3
4

5
a f d

e
PRIMS
vertex distance Dist
ALGORITHM vertex distance Dist
fro fro
m m
A 0 A 0
B 2 A B 0
C 2 A C 1 B
D INF D 5 B

vertex distance Dist


fro
m
A 0
B 0
C 0
D 3 C
Shortest path problem
• Given as input a weighted graph, G=(V,E) and a
distinguished vertex ,s,finding the shortest
weighted path from s to every other vertex in G
is called shortest path problem.
• The general method to solve single source
shortest path problem is known as Dijkstra’s
algorithm.
• Example 2
v1 v2
4 10
1 3
v3
2 v4 2 v5

5 8 4 6
v7
v6
1
Initial configuration

v known dv pv
V1 0 0 0
V2 0 α 0
V3 0 α 0
V4 0 α 0
V5 0 α 0
V6 0 α 0
V7 0 α 0
Starts from v1

v known dv pv
V1 1 0 0
V2 0 2 v1
V3 0 α 0
V4 0 1 v1
V5 0 α 0
V6 0 α 0
v7 0 α 0
Select v4

v known dv pv
V1 1 0 0
V2 0 2 v1
V3 0 3 v4
V4 1 1 v1
V5 0 3 v4
V6 0 9 v4
v7 0 5 v4
Select v2

v known dv pv
V1 1 0 0
V2 1 2 v1
V3 0 3 v4
V4 1 1 v1
V5 0 3 v4
V6 0 9 v4
v7 0 5 v4
Select v5

v known dv pv
V1 1 0 0
V2 1 2 v1
V3 0 3 v4
V4 1 1 v1
V5 1 3 v4
V6 0 9 v4
v7 0 5 v4
Select v3

v known dv pv
V1 1 0 0
V2 1 2 v1
V3 1 3 v4
V4 1 1 v1
V5 1 3 v4
V6 0 8 v3
v7 0 5 v4
Select v7

v known dv pv
V1 1 0 0
V2 1 2 v1
V3 1 3 v4
V4 1 1 v1
V5 1 3 v4
V6 0 6 v7
v7 1 5 v4
Select v6 v known dv pv
V1 1 0 0
V2 1 2 v1
V3 1 3 v4
V4 1 1 v1
V5 1 3 v4
V6 1 6 v7
v7 1 5 v4

V1v2=2
V1v4v3=3
V1v4=1
V1v4v5=3
V1v4v7v6=6
V1v4v7=5
• Example 2
v1 v2
4 10
1 3
v3
2 v4 2 v5

5 8 4 6
v7
v6
1
Floyd’s Algorithm: All pairs shortest paths
Problem: In a weighted (di)graph, find shortest paths between
every pair of vertices
idea: construct solution through series of matrices D(0), …,
D (n) using increasing subsets of the vertices allowed
as intermediate

Example:
4 3
1
1
6 111
1 5
4
2 3
Floyd’s Algorithm (matrix generation)

On the k-th iteration, the algorithm determines shortest


paths between every pair of vertices i, j that use only
vertices among 1,…,k as intermediate

D(k)[i,j] = min {D(k-1)[i,j], D(k-1)[i,k] + D(k-1)[k,j]}

D(k-1)[i,k]
k

D(k-1)[k,j]
D(k-1)[i,j]
j
Floyd’s Algorithm (example)
1 2 3 4
1
2 2 1 0 ∞ 3 ∞
2 2 0 ∞ ∞
3 6 7
D(0) = 3 ∞ 7 0 1
3 4 4 6 ∞ ∞ 0
1
Floyd’s Algorithm (example)
1 2 3 4
1
2 2
1 0 ∞ 3 ∞
3 6 7 2 2 0 5 ∞
D(1) = 3 ∞ 7 0 1
3 4 4 6 ∞ 9 0
1
413
213
Floyd’s Algorithm (example)
1
2 2

3 6 7

3 4
1
321
3213
0 ∞ 3 ∞
2 0 5 ∞
D(2) = 9 7 0 1
6 ∞ 9 0
Floyd’s Algorithm (example)
1
2 2
4134
3 6 7 132
134
3 4
1 2134
4132

0 10 3 4
2 0 5 6
D(3) = 9 7 0 1
6 16 9 0
Floyd’s Algorithm (example)
1
2 2
341
3 6 7 3413
1341
3 4
1 21341

0 10 3 4
2 0 5 6
D(4) = 7 7 0 1
6 16 9 0
Floyd’s Algorithm (pseudocode and analysis)

You might also like