2. Introduction
• Graph is a collection of two finite sets (V and E)
where V contains the finite number of elements
called vertices(nodes) and E contains the finite
number of elements called the edges.
• A graph G = (V,E) consists of a finite set of
vertices, V, and a finite set of edges E.
• Each edge is a pair (v,w) where v, w V.
• Node
–has a unique label for identification
2
4. Contd.
• “Graphs” are the mathematical and computer
science abstraction that capture many shared
and common concepts of real-life objects such
as
–networks, e.g.
• electricity
• Internet
–road maps
Etc…
4
6. Terminologies
1. Adjacent vertices: Two vertices are said to be
adjacent if they are connected by an edge.
For example , Band C are adjacent to vertex A
because separate edges connect B and C to A.
2. Adjacent edges: Two edges are said to be
adjacent if they are incident on the common
vertex. For example, edges CD and CE are
adjacent because they are incident on the
common vertex C.
6
7. Contd.
3. Path
–connected sequence of edges:
–e.g. A to B to D to C to E
–if the edges are directed then the path has
to follow the directions of the edges.
4. A cyclic path is a path such that
– There are at least two vertices on the path
– w1 = wn (path starts and ends at same vertex)
i.e. a cycle is a path that goes in a loop from a node
back to itself, and without using the same node
twice 7
8. Contd.
• E.g. A-B-E-A in
B
C
D
E
F
G
A
5. LOOP: It is a special cycle that starts and ends
with the same vertex without visiting any other
vertex.
8
9. contd.
• An acyclic path is a path where each vertex is
unique
• An “acyclic graph” has no cycles.
5. Degree of a vertex: The degree of a vertex is
the number of lines incident on it.
6. Connected vertices: Two vertices are said to
be connected if there is a path between them.
7. Out-degree: The out-degree of a directed
graph is the number of arcs leaving the vertex.
8. In-degree: The in-degree of a directed graph
is the number of arcs entering the vertex.
9
11. 11
Contd.
Example: What are the in-degrees and out-degrees of the
vertices a, b, c, d in this graph:
a
b
c
d
deg-(a) = 1
deg+(a) = 2
deg-(b) = 4
deg+(b) = 2
deg-(d) = 2
deg+(d) = 1
deg-(c) = 0
deg+(c) = 2
12. Types of Graph
1. Simple graph: A graph that does not contain
any loop or multiple edges between two
vertices is called a simple graph.
A B
C
D
E F
12
13. Contd.
2. Multi-graph: A graph that contains more than
one edge between two vertices is called a
multi-graph.
Example: B
C
D
E F
13
14. Contd.
3. Directed graph: In a directed graph, the edges
are arrows.
• Directed graphs show the flow from one node
to another and not vise versa.
14
15. Contd.
4. Undirected Graphs: In an undirected graph,
the edges are lines.
• Undirected graphs show a relationship
between two nodes.
15
16. Contd.
5. Weighted graph: A graph is known as a
weighted graph if a weight or metric is
associated with each edge.
16
17. Contd.
6. Complete graph: An undirected graph that
has an edge between every pair of vertices is
called a complete graph.
• Note: A directed graph can also be a complete
graph; in that case, there must be an edge
from every vertex to every other vertex.
17
18. Representation of Graph
• Graphs can be represented by their adjacency
matrix or an edge (or vertex) list. i.e.
1. Sequential representation using adjacency
matrices.
2. Linked representation using adjacent list or
linked list of neighbors.
Adjacency matrices have a value ai,j = 1 if
nodes i and j share an edge; 0 otherwise.
18
20. Sequential Representation using
Adjacency Matrix
An undirected graph and its adjacency matrix representation.
A directed graph and its adjacency matrix representation.
A
B
C
D
E
A
A B C D E
A 0 1 1 0 0
B 0 0 0 1 0
C 0 1 0 0 1
D 0 0 0 0 1
E 0 0 0 0 0
20
21. Traversal of Graph
• Some application of the graph require
systematically visiting all the nodes.
• In a graph ,we can reach to the vertex from
multiple paths existing in the graph, so we can
say that graph traversal is not as simple as
traversal in other data structure.
• There are two standard traversal methods
generally used with graph:
1. Breadth first search(BFS)
2. Depth first search(DFS)
21
22. Breadth first search(BFS)
• BFS is that it starts traversing the graph from the
starting node (any selected node) and then traverses all
the nodes which are directly connected to the starting
node in a particular order.
• First, the starting node is examined and all the adjacent
nodes are placed into the queue.
• The node traversed is marked as visited.
• The next node is taken from the front of the queue and
examined;
• This node also marked as visited and all the neighbors
of this node are placed into the rear of the queue.
• In BFS all the nodes at a particular level are processed
first, only then can we move to the next level.
22
23. BFS algorithm
1. Start traversing the given graph with the given
number of vertices and edges
2. Insert the starting source vertex S to the queue
and mark it as visited
3. Repeat
a. Remove the front node K of the queue and
examine it.
b. Insert all the adjacent nodes of K (which are not
visited so far)to the queue and mark them as
visited
4. Until queue is empty
5. Exit
23
25. Solution
Step 1: start traversing the graph given.
Step 2: Insert the starting source vertex E to the
queue and mark it as visited.
Step 3: Take the front node E. Examine it.
Step 4: insert all the adjacent nodes of E to queue
named as Q and mark them as visited.
25
E
0 1 2 3 4 5
F=0,R=0
B F
0 1 2 3 4 5
F=0,R=1
26. Contd.
Step 5: take the front node B. examine it.
Step 6: insert all the adjacent nodes of B to Q
and mark them as visited.
26
F
0 1 2 3 4 5
F=1,R=1
F A C D
0 1 2 3 4 5
F=1,R=4
27. Contd.
Step 7: take the front node F. examine it.
Step 8: insert all the adjacent nodes of F to Q
and mark them as visited.
Step 9: take the front node A. examine it.
27
A C D
0 1 2 3 4 5
F=2,R=4
A C D
0 1 2 3 4 5
F=2,R=4
C D
0 1 2 3 4 5
F=3,R=4
28. Contd.
Step 10: insert all the adjacent nodes of A to Q
and mark them as visited.
Step 11: take the front node C. examine it.
Step 12: insert all the adjacent nodes of C to Q
and mark them as visited.
28
C D
0 1 2 3 4 5
F=3,R=4
D
0 1 2 3 4 5
F=4,R=4
29. Contd.
.
Step 13: take the front node D. examine it.
Step 14: insert all the adjacent nodes of D to Q
Step 15: stop now because Q is empty
The BFS order is EBFACD
29
D
0 1 2 3 4 5
F=4,R=4
0 1 2 3 4 5
F=5,R=4
30. Depth First search
• The traversal starts at the starting source vertex
and traverses all the nodes along the path
which begins at the source vertex.
• The implementation of the DFS is similar to the
BFS except that stack is used in place of queue.
30
31. DFS algorithm
1.Start traversing the given graph with the given
number of vertices and edges.
2. Push the starting source vertex S to the stack and
mark it as visited.
3. Repeat
a. Remove the top node K of the stack and
examine it.
b. Push all adjacent nodes of K(which are not
visited so far)to the stack and mark them as
visited
4. Until stack is empty
5. Exit
31
33. solution
Step 1: Start traversing the given graph with
number of vertices and edges.
Step 2: Push the starting source vertex A into
the vertex and mark it as visited.
33
Top A
34. Contd.
Step 3: take away(pop) the top node A. examine it.
Step 4: Push all the adjacent node of A to the stack
in any order and mark them as visited.
34
B
C
Top
35. Contd.
Step 5: take away(pop) the top node B. examine it.
Step 6: Insert all the adjacent node of B to the stack
in any order and mark them as visited.
35
D
E
C
Top
36. Contd.
Step 7: take away(pop) the top node D. examine it.
Step 8: Insert all the adjacent node of D to the
stack. B and C are already marked as visited
36
F
E
C
Top
37. Contd.
Step 9: take away(pop) the top node F. examine it.
Step 10: Insert all the adjacent node of F to the
stack. D, C and E are already marked as visited
37
E
C
Top
38. Contd.
Step 11: take away(pop) the top node E. examine it.
Step 12: Insert all the adjacent node of F to the
stack. B and F are already marked as visited
38
C
Top
39. Contd.
Step 13: take away(pop) the top node C. examine it.
Step 14: Insert all the adjacent node of F to the
stack. A,B ,D and F are already marked as visited
39
Top=NULL
40. Contd.
Step 15: Stop now because stack is empty
Therefore, The DFS order is ABDFEC
40
41. The Minimum Spanning Tree
• Any tree consisting of all the vertices of a graph is
called a spanning tree.
• A spanning tree of an undirected graph G is a subgraph
of G that is a tree containing all the vertices of G.
• In a weighted graph, the weight of a subgraph is the
sum of the weights of the edges in the subgraph.
• A minimum spanning tree (MST) for a weighted
undirected graph is a spanning tree with minimum
weight.
• A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that
it is a tree (i.e., it is acyclic)
it covers all the vertices V
41
42. Applications of MST
• Any time you want to visit all vertices in a
graph at minimum cost (e.g., wire routing on
printed circuit boards, sewer pipe layout, road
planning…)
• There are two different algorithms to find out
the MST:
1. Prim’s algorithm
2. Kruskal algorithm
42
43. Minimum Spanning Tree: Prim's Algorithm
• Prim's algorithm for finding an MST is a greedy algorithm.i.e.
Constructs a solution to an optimization problem piece by
piece through a sequence of choices that are:
• locally optimal (with respect to some neighborhood
definition)
• greedy (in terms of some measure), and irrevocable
• Start by selecting an arbitrary vertex, include it into the
current MST.
• Grow the current MST by inserting into it the vertex
closest(incident) to one of the vertices already in current MST.
43
44. Example
• Let us take the graph shown in figure below.
And find out the MST using Prim’s algorithm.
44
B
C
D
E
F
A
2
2
2
3
3
4
3
7
6
45. Solution
Step 1: Select the vertex A. Now V={A}
Step 2: The edge incident on A are AB and AC. So
add the minimum of AB and AC.
Min{AB,AC}=Min{3,4}=3=AB
Now V={A,B} 45
A
B
A
3
46. Contd.
Step 3: Find the edge with minimum cost incident on
either A or B. AB and AC are the edges incident on
A and BC ,BD and BE are incident on B. since AB is
already included so ignore it. There fore
,Min{AC,BD,BE,BC}=Min{4,2,7,3}=2=BD. Add BD.
Now V={A,B,D}
46
B
A
3
D
2
47. Contd.
Step 4: find the edge with minimum cost incident
on A,B or D. AB and AC are the edges incident on
A; BC,BD and BE are incident on B .And BD, CD
and DF are incident on D. Since AB and BD are
already included so ignore them. Therefore,
Min{AC,BE,BC,CD,DF}=Min{4,7,3,6,2}=2=DF. Add
DF.
Now V={A,B,D,F} 47
B
A
3
D
2
F
2
48. Contd.
Step 5: Find the edge with minimum cost incident
on A,B,D or F. The edges incident on these
vertices and which are not included in the tree
are AC,BC,BE,DC,EF. Therefore,
Min{AC,BC,BE,DC,EF}=Min{4,3,7,6,2}=2=EF. So add
this edge.
48
B
A
3
D
2
F
E
2
2
Now V={A,B,D,F,E}
49. Contd.
Step 6: Find the edge with minimum cost incident on A,B,D ,F
or E. The edge incident on these vertices and which are
not included in the tree are AC,BC,BE,DC,EC. Therefore,
Min{AC,BC,BE,DC,EC}=Min{4,3,7,6,3}=3={BC,EC}
Select any one(BC) from this set.
Now V={A,B,C,D,E,F}.
step 7: Stop, since all 5 edges are included. 49
B
A
3
D
2
F
E
2
2
C
3
50. Exercise
• Find the MST for the following graph using
Prim's Algorithm.(select vertex b first)
50
51. Minimum Spanning Tree: Kruskal
algorithm
This algorithm was developed by Joseph Kruskal.
The kruskal algorithm creates the MST T by adding
the edge one at a time to T.
A minimum cost spanning tree T is built edge by
edge.
We start with the edge of minimum cost.
If there are several edges with the same minimum
cost, then we select any one of them and add it to
the spanning tree T, provided its addition does not
form a cycle.
We then add an edge with next lowest cost and so
on. 51
52. Example
• Let us take the graph shown in figure below to
find out the MST using Kruskal algorithm.
52
B
C
D
E
F
A
2
2
2
3
3
4
3
7
6
53. Solution
Step 1: The edges with minimum cost are BD,DF
and EF. Select any one.
Step 2: The edges with the next minimum cost
are BD and DF. Select any one.
53
E
F
2
E
F
2
D
2
54. Contd.
Step 3: The edges with the next minimum cost is
BD . So add it if it does not form a cycle.
54
E
F
2
D
2
B 2
55. Contd.
Step 4: The edges with the next minimum cost are
AB,BC and CE . So add any one that does not
form a cycle.
55
E
F
2
D
2
B 2
A
3
56. Contd.
Step 5: The edges with the next minimum cost
are BC and CE . So add any one that does not
form a cycle.
Step 6: Stop, since 5 edges are included.
56
E
F
2
D
2
B
2
A
3
C
3
57. Single-Source Shortest Path Problem
Single-Source Shortest Path Problem - The
problem of finding shortest paths from a source
vertex v to all other vertices in the graph.
There are various algorithms to find out the
shortest path.
57
58. Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-
source shortest path problem in graph theory.
Works on both directed and undirected graphs.
However, all edges must have nonnegative
weights.
Input: Weighted graph G={E,V} and source vertex
v∈V, such that all edge weights are nonnegative
Output: Lengths of shortest paths (or the shortest
paths themselves) from a given source vertex
v∈V to all other vertices
58
59. Algorithm: Dijkstra’s algorithm
1. Set p={starting vertex S} and T={V}
2. Assign the index value 0 to the starting vertex S=0
and ∞ to all other vertices.
3. Repeat
a. Take the vertex v1 with minimum index value,
delete it from T and mark it as visited.
b. Find the new index value of adjacent vertex v2
with respect to v1 as follows:
Index(v2)=Min{Index(v2),Index(v1)+w(v1,v2)}
4. Until T is empty
59
60. Dijkstra's algorithm - Pseudocode
dist[s] ←0 (distance to source vertex is zero)
for all v ∈ V–{s}
do dist[v] ←∞ (set all other distances to infinity)
S←∅ (S, the set of visited vertices is initially empty)
Q←V (Q, the queue initially contains all
vertices)
while Q ≠∅ (while the queue is not empty)
do u ← mindistance(Q,dist) (select the element of Q with the min.
distance)
S←S∪{u} (add u to list of visited vertices)
for all v ∈ neighbors[u]
do if dist[v] > dist[u] + w(u, v) (if new shortest path found)
then d[v] ←d[u] + w(u, v) (set new value of shortest path)
(if desired, add traceback code)
return dist
60
61. Example
• Find the shortest path from the vertex A.
61
A B
C D
E
F
2
8
5
6 3
6
5
3
62. Contd.
Step 1: The index value of the source vertex A is
assign to 0 and index value of other vertices to
infinity T={A,B,C,D,E,F}
62
Vertex A B C D E F
Index
value
0 ∞ ∞ ∞ ∞ ∞
T A B C D E F
63. Contd.
Step 2: Select the vertex A with minimum value and
delete A from T. So T={B,C,D,E,F}
Find the new index value of all adjacent vertices of
A. Therefore
Newindex(B)=Min{Index(B),Index(A)+w(A,B)={∞,0+2}=2
Newindex(C)=Min{Index(C),Index(A)+w(A,C)={∞,0+5}=5
Newindex(E)=Min{Index(E),Index(A)+w(A,E)={∞,0+3}=3
63
Vertex A B C D E F
Index
value
0 2 5 ∞ 3 ∞
T B C D E F
64. Contd.
Step 3: Select the vertex B with minimum index
value and delete B from T. So T={C,D,E,F}
Find the new index value of all adjacent vertices of
B,that is A,E,D. However, A is not the member of
T so
Newindex(D)=Min{Index(D),Index(B)+w(B,D)={∞,2+8}=10
Newindex(E)=Min{Index(E),Index(B)+w(B,E)={3,2+5}=3
64
Vertex A B C D E F
Index
value
0 2 5 10 3 ∞
T C D E F
65. Contd.
Step 4: Select the vertex E with minimum index
value and delete Efrom T. So T={C,D,F}
Find the new index value of all adjacent vertices of
E, that is A,B,F. However, A and B are not the
member of T so
Newindex(F)=Min{Index(F),Index(E)+w(E,F)={∞,3+6}=9
65
Vertex A B C D E F
Index
value
0 2 5 10 3 9
T C D F
66. Contd.
Step 5: Select the vertex C with minimum index
value and delete C from T. So T={D,F}
Find the new index value of all adjacent vertices of
C, that is A,F. However, A is not the member of T
so
Newindex(F)=Min{Index(F),Index(C)+w(C,F)={9,5+6}=9
It remains unchanged.
66
Vertex A B C D E F
Index
value
0 2 5 10 3 9
T D F
67. Contd.
Step 6: Select the vertex F with minimum index
value and delete F from T. So T={D}
Find the new index value of all adjacent vertices of
F, that is C,E,D. but C and E are not the member
of T so
Newindex(D)=Min{Index(D),Index(F)+w(F,D)={10,9+3}=10
It remains unchanged.
67
Vertex A B C D E F
Index
value
0 2 5 10 3 9
T D
68. Contd.
Step 7: Select the vertex D with minimum index value and
delete D from T. So T={}. Since this is the last vertex so stop.
68
Vertex A B C D E F
Index
value
0 2 5 10 3 9
T
Therefore, now we can see that the shortest path between
A and B = AB of cost 2
A and C = AC of cost 5
A and D = ABD of cost 10
A and E =AE of cost 3
A and F = AEF of cost 9