DFS3
DFS3
Graph
A graph G = (V, E); V is a set of nodes and E is a set of edges.
An edge ’x’ in E is associated with a pair of nodes denoted by ’u’ and
’v’. That is, edge x connects ’u’ and ’v’.
Adjacent nodes
Any two nodes which are connected by an edge in a graph are called
adjacent node.
Directed graph
A graph in which every edge is directed is called directed graph.
Clearly, (u, v ) denoting a pair of nodes in G , there would be a path
from u to v and also a path from v to u.
Undirected graph
A graph in which every edge is undirected is called undirected graph.
Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 1 / 23
Mixed Graph
A graph involving the edges which are both directed or undirected is called
mixed graph.
Loop
An edge of graph which joins a node to itself is called a loop (sling).
Parallel Edges
In a graph (directed or un directed), edges between pairs of nodes that
exist in parallel are parallel edges.
Multigraph
Any graph which contains some parallel edges or self loops are multigraph.
Weighted graph
A graph with weights associated with the edges is called weighted graph.
Cyclic graph
A path containing one or more edges in which it starts from a vertex v and
terminates into the same vertex is called cyclic graph.
Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 3 / 23
Acyclic graph
A graph which does not hold any cycle is called acyclic graph.
Complete graph
A graph G is said to be completed if every node in G is adjacent to every
other node. A complete graph with n nodes will have n(n − 1)/2 edges.
Representation of graphs
Adjacency matrix
Adjacency list
Incidence matrix
Adjacency matrix: A n × n matrix, (aij ), is in the form of adjacency
matrix aij = 1, if there is an edge from vi to vj
= 0, otherwise.
Incidence matrix
For a graph G with n vertices, e edges and no self loops, incidence matrix
A of the size n × e is defined as
aij = 1, if there is an edge j incident to node vi
= 0, otherwise.
a b c d e f g h i j k l
A 1 0 0 0 0 0 1 0 0 0 0 0
B 1 1 1 0 0 0 0 0 0 0 0 0
C 0 1 0 1 0 0 1 1 0 0 1 0
D 0 0 1 1 1 1 0 0 0 0 0 0
E 0 0 0 0 1 0 0 1 1 1 0 0
F 0 0 0 0 0 0 0 0 0 1 1 1
G 0 0 0 0 0 1 0 0 1 0 0 1
A
A BCF A
F BCD AF
D BCEJ AF D
J BCEK AF D J
K BCEG AF D J K
G BCE AF D J K G
E BC AF D J K GE
C B AF D J K GEC
B Empty AF D J K GECB
A
A FCB A
F CBD AF
C BDEG AF C
B DEG AF C B
D EGJ AF C B D
E GJK AF C B D E
G JK AF C B D EG
J K AF C B D EGJ
K Empty AF C B D EGJK
Kruskals algorithm
Take a graph with ’n’ vertices.
Make the tree T empty.
Sort all the edges in non-decreasing order of their weight.
Repeat the following three steps as T contains n - 1 edges and a set
of edges E is non empty.
Choose an edge (v , w ) from ECBNST
Dr. Avatharam Ganivada (SCIS) with the lowest cost. March 13, 2018 14 / 23
Delete the edge (v , w ) from E .
If the edge (v , w ) does not create a cycle in T then add (v , w ) to T ,
otherwise discard (v , w ).
If T contains less than n - 1 edges then print no spanning tree exists.
Example1:
Cost 10 15 20 25 30 35 40 45 50 55
Edge (1, 2) (3,6) (4,6) (2,6) (1,4) (3,5) (2,5) (1,5) (2,3) (5,6)
Example2: