0% found this document useful (0 votes)
28 views30 pages

Unit 5 DS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views30 pages

Unit 5 DS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT-5

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

 A directed graph or digraph is one in which the edges have a direction.

 An undirected graph is one in which the edges do not have a direction.

 The size of a graph is the number of nodes in it

 The empty graph has size zero (no nodes).

 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.

For directed graphs:


The in-degree of a node is the number of in-edges it has.
The out-degree of a node is the number of out-edges it has.

An undirected graph is connected if there is a path from every


node to every other node.

A directed graph is strongly connected if there is a path from


every node to every other node.
 A path with no repeated vertices is called a simple path.

 A cycle in G is a simple path in which the first and last vertices are the same.

 A graph without cycles is called acyclic graph.

 Sub graph is a graph with subset of vertices and edges of a graph.

 A graph is called a simple graph if it has no loops and no parallel edges.

 A graph is termed as weighted graph if all the edges in it are labeled with
some weights.

 A graph is called complete graph if there is an edge between every pair of


nodes/vertices .
Directed Un Directed
if edges ordered pairs (u,v) if edges unordered pairs {u,v}

 
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

Complete Graph Strongly Connected

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)

(a) Some of the sub graph of G1

0
0 0 0
1 1 1
2 2
G2 (i) (ii) (iii)

(b) Some of the sub graph of G2


Representation of a Graph

There are two ways of representing a graph in memory:

Sequential Representation by means of Adjacency Matrix.

Linked Representation by means of Linked List.


 Adjacency Matrix is a bit matrix which contains entries of only 0 and 1

 The connected edge between two vertices is represented by 1 and absence


of edge is represented by 0.

 This representation uses a square matrix of order n x n, where n is the


number of vertices in the graph.

 Adjacency matrix of an undirected graph is symmetric.

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

 It saves the memory.

 The number of lists depends on the number of vertices in the graph.

 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

any one vertex which is adjacent to the current vertex is traversed.

To implement the depth first search algorithm, we use a stack.


DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the current node
2. Find an unvisited neighbor of the current node, visit it, and make it the
new current node;
3. If the current node has no unvisited neighbors, backtrack to the its
parent, and make that parent the new current node.
4. Repeat steps 2and 3 until no more nodes can be visited.
5. If there are still unvisited nodes, repeat from step 1.
Breadth First Traversal:
The breadth first traversal is similar to the pre-order traversal of a binary tree.

The breadth first traversal of a graph is similar to traversing a binary tree


level by level (the nodes at each level are visited from left to right).

All the nodes at any level, i, are visited before visiting the nodes at level i + 1.

To implement the breadth first search algorithm, we use a queue.


BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root in a BFS tree being
formed. Its level is called the current level.
2. From each node x in the current level, visit all the unvisited neighbors of
x. The newly visited nodes from this level form a new level that becomes
the next current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
Example1: A
Graph

B C D

B C D
A

E
B C D
The Depth First Search Tree Order : A, B, E, D, C

The Breadth First Search Tree Order : A, B, C, D, E


Example2:

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.

Depth First Search:

Given Graph From start vertex A explore edges.

From vertex B either C or D to be explored.


Since C is dead end, backtrack From D it is possible to explore A, but this
to B, from there explore D. would form a cycle, so again backtrack to B,
from there backtrack to A, explore the path to F.
, F
D
, C,
A ,B
:
d er
O r
r ee
T
F S
e D From F it is possible to traverse either A or c, but
Th both are discovered already. So F is also a dead end.

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

From the start vertex A, the explored


Given Graph.
vertices are B and F.

Explore all paths from vertex B and F.


The dashed lines indicate, nodes are From D the explored vertex is A.
previously discovered. But A is already visited.

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.

Graph A Some Spanning Trees from Graph A

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.

Weighted Graph Minimum Spanning Tree


7

2 2
5 3 3
4

1 1

Algorithms to find Minimum Spanning Trees are:


 Kruskal‘s Algorithm
 Prim‘s Algorithm
Kruskal’s algorithm

Kruskal’s algorithm finds the minimum cost spanning tree by selecting the
edges one by one as follows.

1. Draw all the vertices of the graph.


2. Select the smallest edge from the graph and add it into the spanning tree
(initially it is empty).
3. Select the next smallest edge and add it into the spanning tree.
4. Repeat the 2 and 3 steps until that does not form any cycles.
5
A B
4 6 2

C 2 D 3

3 1 2
E F
4

Minimum Spanning Tree for the above graph is:

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.

1. All vertices are marked as not visited

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.

4. The process is repeated until a spanning tree is formed.


5
A B
4 6 2

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy