Unit 5 DS
Unit 5 DS
Graphs
Binary Trees
Terminology
Representation
Tree Traversals
Introduction to Graphs
Definition: A graph G is a pair, G = (V, E), where V is a finite nonempty
set of vertices and E is called the set of edges.
Example: a b
d e
V= {a,b,c,d,e}
E=(a,b), (a,c), (a,d), (b,e), (c,d), (c,e), (d,e)}
Graph Terminology
If two nodes are connected by an edge, they are neighbors (and the nodes are
adjacent to each other).
A path is a sequence of nodes such that each node (but the last) is the
predecessor of the next node in the list.
Example: If v1,v2,. . .,vk are vertices then vi and vi+1 should be consecutive.
The degree of a node is the number of edges it has.
A cycle in G is a simple path in which the first and last vertices are the same.
A graph is termed as weighted graph if all the edges in it are labeled with
some weights.
u v
u v
3 G2 0 in:1, out: 1
G1
0
3 1 2 3 1 in: 1, out: 2
3 3
2 in: 1, out: 0
Cyclic graph Acyclic graph Weighted Graph
A A
A
10 9
B C B C
B C
6
D D
A B A
D B
C D
C
E
0 0 0 1 2 0
1 2 1 2 3 1 2
3 3
G1
(i) ii) (iii) (iv)
0
0 0 0
1 1 1
2 2
G2 (i) (ii) (iii)
Adjacency Matrix
A
A B C D E
A 0 1 1 1 0
B C
B 1 0 1 1 0
C 1 1 0 1 1
D E D 1 1 1 0 1
E 0 0 1 1 0
Linked List Representation
The header node in each list maintains a list of all adjacent vertices of a
node .
A B C D NULL
A
B A C D NULL
D E NULL B C
C A B
D A B C E NULL D E
N E C D NULL
Undirected Graph Adjacency List Adjacency Matrix
Directed Graph Adjacency List Adjacency Matrix
Graph Traversal Techniques
There are two standard graph traversal techniques:
Depth-First Search (DFS)
Breadth-First Search (BFS)
Traversing a graph means visiting all the vertices in the graph exactly once.
DFS and BFS traversals result an acyclic graph.
DFS and BFS traversal on the same graph do not give the same order of visit
of vertices.
Depth First Traversal:
The depth first traversal is similar to the in-order traversal of a binary tree.
An initial or source vertex is identified to start traversing, then from that vertex
All the nodes at any level, i, are visited before visiting the nodes at level i + 1.
B C D
B C D
A
E
B C D
The Depth First Search Tree Order : A, B, E, D, C
A B C A B C
D E F D E F
G H I G H I
DFS Traversal Order BFS Traversal Order
A B C F E G D H I A B D E C G F H I
Example3: Construct the DFS and BFS for the following graph.
Note: From the above diagram it is possible to say that G and E are never traversed.
Breadth First Search: The BFS Tree Order : A,B,F,C,D
Note: From the above diagram it is possible to say that G and E are never traversed.
Applications of Graphs
Electronic circuits
Printed circuit board
Integrated circuit
Transportation networks
Highway network
Flight network
Computer networks
Local area network
Internet
Web
Databases
Entity-relationship diagram
Spanning Trees
A spanning tree of a graph is just a sub graph that contains all the vertices and
is a tree.
A graph may have many spanning trees.
o o o
r r r
Minimum Spanning Trees
The minimum spanning tree for a given graph is the spanning tree of
minimum cost for that graph.
2 2
5 3 3
4
1 1
Kruskal’s algorithm finds the minimum cost spanning tree by selecting the
edges one by one as follows.
C 2 D 3
3 1 2
E F
4
A B
2
C 2 D
2
3 1
E F
Prim’s algorithm
Prim’s algorithm finds the minimum cost spanning tree by selecting the
edges one by one as follows.
2. Any vertex v you like is chosen as starting vertex and is marked as visited
(define a cluster C).
3. The smallest- weighted edge e = (v,u), which connects one vertex v inside
the cluster C with another vertex u outside of C, is chosen and is added to
the MST.
C 2 D 3
3 1 2
E F
4
Adjacency matrix Minimum Spanning Tree for the above graph is:
A B C D E F A B
A - 5 4 6 2 - 2
B 5 - - 2 - 3
C 4 - - - 3 - C 2 D
D 6 2 - - 1 2
E 2 - 3 1 - 4 3 1 2
F - 3 - 2 4 - E F
Exercise:
1 5
6
1
5 4
2 5
3 2
3 4
6
6
5 6