Graph
Prepared By : Ketaki Pattani
Enroll. No. : 130210107039
1
4
76
3 5
2
1 2
3
Some related terms:
• Graph: A graph G is a pair (V,E), where V is a finite
set of points called vertices and E is a finite set of
edges.
• Path: A path from a vertex v to a vertex u is a
sequence <v0,v1,v2,…,vk> of vertices where v0 ,v1,
v2… are the set of vertices followed to move from a
source to destination.
• Pathlength:The length of a path is defined as the
number of edges in the path.
Related terms: Degree of vertex
• Degree of vertex: It represents
the total number of edges linked
to a particular vertex.
• Indegree: It is the number of
incomming edges to a vertex.
•Outdegree: It is the number of
outgoing edges from a vertex.
•Source: A vertex having indegree
zero is a source.
•Sink: A vertex having outdegree
zero is a sink.
Some related terms:
• Undirected Graph: An
undirected graph is the
graph consisting of
unordered pair of vertices .
For eg :figure (a)
• Directed Graph : In a
directed graph, the edge e is
an ordered pair (u,v) which
is incident from vertex u and
is incident to vertex v. For
eg : fig(b)
Some related terms: Weighted graph
• A weighted graph is a graph
where each edge is assigned
a particular value.
• This may be the distance or
the cost.
• Here, the graph G(V,E) may
be directed or undirected
graph.
• Also the weight may be
represented by ‘W’.
Graph Representation:
Adjacency Matrix:
• Here, a two dimensional
square matrix can be used
to store a graph.
• Adjacency matrix for
undirected graph is always
symmetric .
Adjacency List
• Adjacency List creates a
seperste linked list for each
adjacent vertex.
• Adjacency graph for n nodes
is represented by an Array
of Pointers.
a
dc
b a
b
c
d
b
a
d
d c
c
a b
a c
a
dc
b
1 2
3 4
1 2 3 4
1 0 1 1 1
2 1 0 1 0
3 1 1 0 1
4 1 0 1 0
Graph Traversal:
• Most of the applications of graphs require the
traversal of graphs.
• Traversal means visiting each node in a graph
exactly once.
• It also stands for that there must be no cycles in
the traversal.
• The two commonly used techniques are:
(1) Depth First Search(DFS)
(2) Breadth First Search(BFS)
Breadth First Search:
• Strategy
 choose a starting vertex,
distance d = 0
 vertices are visited in order
of increasing distance from
the starting vertex,
 examine all edges leading
from vertices (at distance
d) to adjacent vertices (at
distance d+1)
 then, examine all edges
leading from vertices at
distance d+1 to distance
d+2, and so on,
 until no new vertex is
discovered
Algorithm for BFS:
1. for each vertex u in V[G] – {s}
2 do color[u]  white
3 d[u]  
4 [u]  nil
5 color[s]  gray
6 d[s]  0
7 [s]  nil
8 Q  
9 enqueue(Q,s)
10 while Q  
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
Example (BFS)
 0
  
 

r s t u
v w x y
Q: s
0
(Courtesy of Prof. Jim Anderson)
Example (BFS)
1 0
1  
 

r s t u
v w x y
Q: w r
1 1
Example (BFS)
1 0
1 2 
2 

r s t u
v w x y
Q: r t x
1 2 2
Example (BFS)
1 0
1 2 
2 
2
r s t u
v w x y
Q: t x v
2 2 2
Example (BFS)
1 0
1 2 
2 3
2
r s t u
v w x y
Q: x v u
2 2 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: v u y
2 3 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: u y
3 3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: y
3
Example (BFS)
1 0
1 2 3
2 3
2
r s t u
v w x y
Q: 
Use of a queue in BFS Graph:
• It is very common to use a queue to keep
track of:
– nodes to be visited next, or
– nodes that we have already visited.
• Typically, use of a queue leads to a breadth-
first visit order.
• Breadth-first visit order is “cautious” in the
sense that it examines every path of length i
before going on to paths of length i+1.
Depth First Search:
• Strategy
– choose a starting vertex,
distance d = 0
– examine One edges leading
from vertices (at distance d)
to adjacent vertices (at
distance d+1)
– then, examine One edges
leading from vertices at
distance d+1 to distance d+2,
and so on,
– until no new vertex is
discovered, or dead end
– then, backtrack one distance
back up, and try other edges,
and so on.
Algorithm for DFS:
.
Example (DFS)
1/
u v w
x y z
Example (DFS)
1/ 2/
u v w
x y z
Example (DFS)
1/
3/
2/
u v w
x y z
Example (DFS)
1/
4/ 3/
2/
u v w
x y z
Example (DFS)
1/
4/ 3/
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/7
u v w
x y z
B
Example (DFS)
1/
4/5 3/6
2/7
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7 9/
u v w
x y z
BF
Example (DFS)
1/8
4/5 3/6
2/7 9/
u v w
x y z
BF C
Example (DFS)
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
BF C
Example (DFS)
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
BF C
B
Example (DFS)
1/8
4/5 3/6 10/11
2/7 9/
u v w
x y z
BF C
B
Example (DFS)
1/8
4/5 3/6 10/11
2/7 9/12
u v w
x y z
BF C
B
Use of a stack in DFS Graph
• It is very common to use a stack to keep track
of:
– nodes to be visited next, or
– nodes that we have already visited.
• Typically, use of a stack leads to a depth-first
visit order.
• Depth-first visit order is “aggressive” in the
sense that it examines complete paths.
Minimum Spanning Tree (MST)
• it is a tree (i.e., it is acyclic)
• it covers all the vertices V
– contains |V| - 1 edges
• the total cost associated with tree edges is the
minimum among all possible spanning trees
A minimum spanning tree is a subgraph of an
undirected weighted graph G as shown, such that
Minimum Spanning Tree: Prim's
Algorithm
• Prim's algorithm for finding an MST 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.
Minimum Spanning Tree: Prim's Algorithm
.
Minimum Spanning Tree: Prim's
Algorithm
.
Dijkstra's algorithm
• For a weighted graph G = (V,E,w), the single-
source shortest paths problem is to find the
shortest paths from a vertex v ∈ V to all other
vertices in V.
• Dijkstra's algorithm is similar to Prim's algorithm.
– maintains a set of nodes for which the shortest paths
are known.
– grow this set based on the node closest to source
using one of the nodes in the current shortest path
set.
Single-Source Shortest Paths: Dijkstra's
Algorithm
.
Floyd's Algorithm
• For any pair of vertices vi, vj ∈ V, consider all paths
from vi to vj whose intermediate vertices belong to
the set {v1,v2,…,vk}.
• Let pi
(
,
k
j
) (of weight di
(
,
k
j
)) be the minimum-weight
path among them.
• If vertex vk is not in the shortest path from vi to vj,
then pi
(
,
k
j
) is the same as pi
(
,
k
j
-1).
• If f vk is in pi
(
,
k
j
), then we can break pi
(
,
k
j
) into two
paths - one from vi to vk and one from vk to vj . Each
of these paths uses vertices from {v1,v2,…,vk-1}.
Floyd's Algorithm
From our observations, the following recurrence
relation follows:
This equation must be computed for each pair of nodes and
for k = 1, n. The serial complexity is O(n3).
Floyd's Algorithm
Floyd's all-pairs shortest paths algorithm.
This program computes the all-pairs shortest paths
of the graph G = (V,E) with adjacency matrix A.
130210107039 2130702

130210107039 2130702

  • 1.
    Graph Prepared By :Ketaki Pattani Enroll. No. : 130210107039 1 4 76 3 5 2 1 2 3
  • 2.
    Some related terms: •Graph: A graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite set of edges. • Path: A path from a vertex v to a vertex u is a sequence <v0,v1,v2,…,vk> of vertices where v0 ,v1, v2… are the set of vertices followed to move from a source to destination. • Pathlength:The length of a path is defined as the number of edges in the path.
  • 3.
    Related terms: Degreeof vertex • Degree of vertex: It represents the total number of edges linked to a particular vertex. • Indegree: It is the number of incomming edges to a vertex. •Outdegree: It is the number of outgoing edges from a vertex. •Source: A vertex having indegree zero is a source. •Sink: A vertex having outdegree zero is a sink.
  • 4.
    Some related terms: •Undirected Graph: An undirected graph is the graph consisting of unordered pair of vertices . For eg :figure (a) • Directed Graph : In a directed graph, the edge e is an ordered pair (u,v) which is incident from vertex u and is incident to vertex v. For eg : fig(b)
  • 5.
    Some related terms:Weighted graph • A weighted graph is a graph where each edge is assigned a particular value. • This may be the distance or the cost. • Here, the graph G(V,E) may be directed or undirected graph. • Also the weight may be represented by ‘W’.
  • 6.
    Graph Representation: Adjacency Matrix: •Here, a two dimensional square matrix can be used to store a graph. • Adjacency matrix for undirected graph is always symmetric . Adjacency List • Adjacency List creates a seperste linked list for each adjacent vertex. • Adjacency graph for n nodes is represented by an Array of Pointers. a dc b a b c d b a d d c c a b a c a dc b 1 2 3 4 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0
  • 7.
    Graph Traversal: • Mostof the applications of graphs require the traversal of graphs. • Traversal means visiting each node in a graph exactly once. • It also stands for that there must be no cycles in the traversal. • The two commonly used techniques are: (1) Depth First Search(DFS) (2) Breadth First Search(BFS)
  • 8.
    Breadth First Search: •Strategy  choose a starting vertex, distance d = 0  vertices are visited in order of increasing distance from the starting vertex,  examine all edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)  then, examine all edges leading from vertices at distance d+1 to distance d+2, and so on,  until no new vertex is discovered
  • 9.
    Algorithm for BFS: 1.for each vertex u in V[G] – {s} 2 do color[u]  white 3 d[u]   4 [u]  nil 5 color[s]  gray 6 d[s]  0 7 [s]  nil 8 Q   9 enqueue(Q,s) 10 while Q   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
  • 10.
    Example (BFS)  0      r s t u v w x y Q: s 0 (Courtesy of Prof. Jim Anderson)
  • 11.
    Example (BFS) 1 0 1     r s t u v w x y Q: w r 1 1
  • 12.
    Example (BFS) 1 0 12  2   r s t u v w x y Q: r t x 1 2 2
  • 13.
    Example (BFS) 1 0 12  2  2 r s t u v w x y Q: t x v 2 2 2
  • 14.
    Example (BFS) 1 0 12  2 3 2 r s t u v w x y Q: x v u 2 2 3
  • 15.
    Example (BFS) 1 0 12 3 2 3 2 r s t u v w x y Q: v u y 2 3 3
  • 16.
    Example (BFS) 1 0 12 3 2 3 2 r s t u v w x y Q: u y 3 3
  • 17.
    Example (BFS) 1 0 12 3 2 3 2 r s t u v w x y Q: y 3
  • 18.
    Example (BFS) 1 0 12 3 2 3 2 r s t u v w x y Q: 
  • 19.
    Use of aqueue in BFS Graph: • It is very common to use a queue to keep track of: – nodes to be visited next, or – nodes that we have already visited. • Typically, use of a queue leads to a breadth- first visit order. • Breadth-first visit order is “cautious” in the sense that it examines every path of length i before going on to paths of length i+1.
  • 20.
    Depth First Search: •Strategy – choose a starting vertex, distance d = 0 – examine One edges leading from vertices (at distance d) to adjacent vertices (at distance d+1) – then, examine One edges leading from vertices at distance d+1 to distance d+2, and so on, – until no new vertex is discovered, or dead end – then, backtrack one distance back up, and try other edges, and so on.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
    Example (DFS) 1/8 4/5 3/6 2/79/ u v w x y z BF C
  • 34.
    Example (DFS) 1/8 4/5 3/610/ 2/7 9/ u v w x y z BF C
  • 35.
    Example (DFS) 1/8 4/5 3/610/ 2/7 9/ u v w x y z BF C B
  • 36.
    Example (DFS) 1/8 4/5 3/610/11 2/7 9/ u v w x y z BF C B
  • 37.
    Example (DFS) 1/8 4/5 3/610/11 2/7 9/12 u v w x y z BF C B
  • 38.
    Use of astack in DFS Graph • It is very common to use a stack to keep track of: – nodes to be visited next, or – nodes that we have already visited. • Typically, use of a stack leads to a depth-first visit order. • Depth-first visit order is “aggressive” in the sense that it examines complete paths.
  • 39.
    Minimum Spanning Tree(MST) • it is a tree (i.e., it is acyclic) • it covers all the vertices V – contains |V| - 1 edges • the total cost associated with tree edges is the minimum among all possible spanning trees A minimum spanning tree is a subgraph of an undirected weighted graph G as shown, such that
  • 40.
    Minimum Spanning Tree:Prim's Algorithm • Prim's algorithm for finding an MST 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.
  • 41.
    Minimum Spanning Tree:Prim's Algorithm .
  • 42.
    Minimum Spanning Tree:Prim's Algorithm .
  • 43.
    Dijkstra's algorithm • Fora weighted graph G = (V,E,w), the single- source shortest paths problem is to find the shortest paths from a vertex v ∈ V to all other vertices in V. • Dijkstra's algorithm is similar to Prim's algorithm. – maintains a set of nodes for which the shortest paths are known. – grow this set based on the node closest to source using one of the nodes in the current shortest path set.
  • 44.
    Single-Source Shortest Paths:Dijkstra's Algorithm .
  • 45.
    Floyd's Algorithm • Forany pair of vertices vi, vj ∈ V, consider all paths from vi to vj whose intermediate vertices belong to the set {v1,v2,…,vk}. • Let pi ( , k j ) (of weight di ( , k j )) be the minimum-weight path among them. • If vertex vk is not in the shortest path from vi to vj, then pi ( , k j ) is the same as pi ( , k j -1). • If f vk is in pi ( , k j ), then we can break pi ( , k j ) into two paths - one from vi to vk and one from vk to vj . Each of these paths uses vertices from {v1,v2,…,vk-1}.
  • 46.
    Floyd's Algorithm From ourobservations, the following recurrence relation follows: This equation must be computed for each pair of nodes and for k = 1, n. The serial complexity is O(n3).
  • 47.
    Floyd's Algorithm Floyd's all-pairsshortest paths algorithm. This program computes the all-pairs shortest paths of the graph G = (V,E) with adjacency matrix A.