A basic tutorial on the elementary graph theory and its implementations using data structures.
This tutorial deals with implementation of graphs in programs using data structures ,traversal algorithms: BFS, DFS, minimal spanning trees, Kruskal's algorithm, Prim's algorithm, Shortest path problem: Dijkastra's algorithm with graphical features.
2. CONTENTS…
INTRODUCTION
START WITH GRAPHS
BASIC TERMINOLOGIES OF GRAPHS
VARIANCE OF GRAPHS
MINIMAL SPANNING TREES
IMPLEMENTATION OF GRAPHS
TRAVERSAL IN GRAPHS: BFS, DFS
KRUSKAL’s ALGORITHM
PRIM’s ALGORITHM
DIZKASTRA ALGORITHM OF SHORTEST PATH
3. INTRODUCTION
This presentation is prepared as a tutorial package on basic graph
theory and algorithms and its implementation as a data structure.
It also includes some assignments for practice on algorithms.
Hope this presentation will help the students to understand the
concepts of elementary graph theory.
PRINCE KUMAR
4. START WITH GRAPHS
A Graph G(V,E) is defined as tuple where_
V ≠ Φ is a finite set of vertices.
E is set of edges between given vertices.
Ex. G(V,E) is a graph as below_
V = {1,2,3,4,5}
E = {
{1,2},{2,3},{3,5},{5,4},{1,4},{2,5}
}
4
5
1
3
2
5. BASIC TERMINOLOGIES OF GRAPHS
Adjacent
In a graph G(V,E) if u and v V such that { u , v } E then u and v are
called adjacent to each other.
Example: in given figure:
2, 3, 4 are adjacent to 5.
4
5
1
3
2
6. BASIC TERMINOLOGIES OF GRAPHS
Degree of a vertex: deg(v)
No. of adjacent to a given vertex is its degree.
Example: in given figure:
vertices 2, 3 and 4 are adjacent to vertex 5
so it has degree 3 i.e. deg(5) = 3 .
4
5
1
3
2
7. BASIC TERMINOLOGIES OF GRAPHS
On the basis of degree, vertices are called_
Isolated : 0 degree , e.g. Vertex 6,
Pendent : 1 degree, e.g. Vertex 3,
Odd : odd degree, e.g. Vertex 5,
Even : even degree, e.g. Vertex 1.
4
5
1
3
2
6
8. BASIC TERMINOLOGIES OF GRAPHS
In a graph ,
The sum of degrees of all the vertices is twice of no. of
edges.
𝑖=1
𝑛
deg 𝑉𝑖 = 2|𝐸|
9. VARIANCE OF GRAPHS
COMPLETE GRAPH
A graph G(V,E) is called as complete graph and denoted as K|V| if
each vertices have degree |V|-1.
Fig. K5
5
1
2
4 3
10. VARIANCE OF GRAPHS
N-REGULAR GRAPH
A graph G(V,E) is called as N- regular graph if each vertices have
degree N.
Fig: 4 Regular Graph
5
1
2
4 3
11. VARIANCE OF GRAPHS
CONNECTED GRAPH
A graph G(V,E) is called connected if in between any two vertices there is a
path else it is disconnected.
Fig: (a) Connected (b) Disconnected
5
1
2
4 3
5
1
2
4 3
6
6
12. VARIANCE OF GRAPHS
WEIGHTED GRAPH
A graph G(V,E) is called weighted if all the edges in the graph
having some cost.
5
1
2
4 3
6
a
b
c
d
ef
g
h
13. VARIANCE OF GRAPHS
SUBGRAPH
A graph S(V’,E’) is called subgraph of graph G(V,E) if V’ is subset of
V and E’ is subset of E.
5
1
2
4 3
6
5
1
2
6
14. VARIANCE OF GRAPHS
SPANNING SUBGRAPH
A graph S(V,E’) is called spanning subgraph of graph G(V,E) if E’ is
subset of E.
5
1
2
4 3
6
5
1
2
4 3
6
15. VARIANCE OF GRAPHS
TREE
A tree is a connected graph with no cycles.
5
1
2
4 3
6
16. MINIMAL SPANNING TREES
A spanning subgraph of a given graph is called its minimal
spanning tree if_
It is a tree.
The total weight on the all the edges in tree is minimal.
5
1
2
4 3
6
2
3
4
1
25
1
3
5
1
2
4 3
6
2
3
1
2
1
17. IMPLEMENTATION OF GRAPHS
A graph is represented in memory by_
Adjacency Matrix : Through 2D arrays.
The adjacency matrix of a graph G(V, E) is defined as _
A(G) = { a i,j }
where a i,j = { 1 or w if (vi, vj) E
0 else
}
20. TRAVERSALS IN GRAPHS
For searching an element in the graph generally
we use two types of traversals as below_
Breadth First Search (BFS):
It searches a node in adjacent of a given node and so on.
It is used when it is known that the target is near to the
source.
21. TRAVERSALS IN GRAPHS
Breadth First Search (BFS) Algorithm:
Let the algorithm assumes A graph G(V,E) is connected is
given with source say root( r ).
It maintains a queue to keep track of the nodes which are
traversed or which are not.
Traversal status or say color of each node shows whether
It is unseen : WHITE , source is yellow.
It is seen only : YELLOW
It is processed : BLACK
22. TRAVERSALS IN GRAPHS
Put the root ‘r’ into the queue;
While(queue ≠ Φ)
u = dequeuer(queue)
for all v in adj_list of u
if color of v is white then
Color v by Yelow.
Enqueue v in queue.
Color u by black
23. TRAVERSALS IN GRAPHS
5
1
2
4 3
1
2
3
4
5
2 5
1 3 4 5
2 4
2 3 5
1 2 4
Lets travers using BFS algorithm in following graph Let source is 1.
QUEUE12 5 3 4
24. TRAVERSALS IN GRAPHS
Depth First Search (DFS):
It searches a node in the path starting of a given node
and until the end of the path, if the target is not in that
path keep backtrack on that path and select a new path
from predecessor nodes and so on .
It is used when it is known that the target is in the depth
of the path starting from the source.
25. TRAVERSALS IN GRAPHS
Depth First Search (BFS) Algorithm:
Let the algorithm assumes A graph G(V,E) is connected is
given with source say root( r ).
It maintains a stack to keep track of the nodes which are
traversed or which are not .
Traversal status or say color of each node shows whether
It is unseen : WHITE , source is yellow.
It is seen only : YELLOW
It is processed : BLACK
26. TRAVERSALS IN GRAPHS
DFS(G)
for each u G.V
Color u white
Set u.Π as Null
for each u G.V
If color of u is white
VISIT(u)
VISIT(u)
Color u yellow
for each v adj_list of u
If color of v is white
Set v. Π as Null
VISIT(v)
Color u black
27. ASSIGNMENT
Q. Traverse the following given graph using DFS algorithm assuming
‘F’ as a source vertex.
A
B
G
F D
E
C
6
4
3
4
7
5
3
2
1
28. KRUSKAL’S ALGORITHM
This algorithm gives MST for a given graph G(V,E).
Steps
Sort all the edges in ascending order by weight and keep then in a Queue.
Make a spanning subgraph say S(V,Φ) of the given graph with no edges.
Dequeue an edge from the queue and add in the S if it’s not making cycle
in the resultant graph.
Follow above step until the queue will be empty.
30. PRIM’S ALGORITHM
This algorithm also grows a MST of a given graph G(V,E) starting with
a source say root (r).
It adds an edge of minimal weight and doesn’t making cycle in the
resultant graph to an isolated vertex.
It uses a min heap(Q) structure to grow vertex with minimal distance
of connecting edge.
A weight function(w) that gives the weight of the edge between two
vertices if exists.
Initial configuration
It assumes that the distance(d) of all other vertices are infinite from the
given source and their predecessor(Π) is NULL.
31. PRIM’S ALGORITHM
Prims_algo(G,w,r)
1. r.d <= 0
2. Q <= G.V
3. while Q is not empty
1. u <= Extract_min(Q)
2. for each v Adj_List[u]
1. If v Q and w(u,v) < v.d
1. v.Π <= u
2. v.d <= w(u,v)
32. ASSIGNMENT
Q. Grow MST for the following graph using Prim’s algorithm assuming
the source is ‘C’.
A
B
G
F D
E
C
6
4
3
4
7
5
3
2
1
33. DIZKASTRA’S ALGORITHM FOR
SHORTEST PATH
It gives shortest path to all the
target vertices in a given
weighted, connected graph
G(V,E) with respect to a given
source vertex say ‘S’.
It maintains a min heap let Q
to maintain the traversal in the
graph.
DIJKASTRA_ALGO(G,S)
Initialize the cost of all the target vertices ∞.
Set the predecessor as Null.
Set cost of S 0.
Get a set R = Φ
PUT(Q, G.V)
WHILE Q is not Null
u = Extract(Q)
R = R U u
For each vertex in adj_list of u
Maintain the lesser cost of paths for the
adjacents of u.
34. ASSIGNMENT
Q.Find out shortest distances for all the vertices in the following graph
assuming the source is ‘C’.
A
B
G
F D
E
C
6
4
3
4
7
5
3
2
1