SlideShare a Scribd company logo
1 of 67
Download to read offline
Graphs: Introduction
 Applications of graphs
 Graph representations
 graph traversals
 Minimal Spanning Trees.
S. Durga Devi ,CSE,CBIT
Introduction to Graphs
 Another non linear data structure.
 In tree structure, there is a hierarchical relationship between
parent and children, that is, one parent and many children.
 On the other hand, in graph, the relationship is from many
parents to many children.
Definition-A graph G = (V,E) is composed of:
V: set of vertices(or nodes)
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:
a b
c
d e
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),(b,e),
(c,d),(c,e),(d,e)}
S. Durga Devi ,CSE,CBIT
S. Durga Devi ,CSE,CBIT
Graph terminology
• Digraph- A digraph is also called a directed graph. It is a graph G, such
that, G=<V,E>, where V is the set of all vertices and E is the set of ordered
pair of elements from V.
• Edges have directions
v= A,B,C,D
E=((A,B),(A,C),(A,D),(D,B))
A B
C
D
A B
C
D
These are directed graphs
G directed if edges ordered pairs (u,v)
 u v
G undirected-
if edges unordered pairs {u,v}
  S. Durga Devi ,CSE,CBIT
Adjacent vertices
If two nodes are connected by an edge, they are neighbors (and the nodes are adjacentto each other).
 The size of a graph is the number of nodes in it
 The empty graph has size zero (no nodes).
Loop-An edge that connects the vertex with itself
Degree of a Vertex- the number of edges connected to that vertex
• Fordirected graph,
– the in-degree of a vertex v is the number of edges incident on to the vertex.
– the out-degree of a vertex v is the number of edges emanating from that vertex.
Examples
3
2
3
3
0
1 33
0
1 2
3 4 5 6
2
3 3
1 1 1 1
directed graph
in-degree
out-degree
0
1
2
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
S. Durga Devi ,CSE,CBIT
 path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent.
a
d e
c
b a b
c
d e
a b e d c
simple path: no vertex is repeated
a b
c
d e
b e c
Cycle - some path with distinct vertices except that the last
vertex is the same as the first vertex
a c d a
a b
c
d e
Parallel edges- if there are one or
More than one edges between
the same pair of vertices.
S. Durga Devi ,CSE,CBIT
 A graph without cycles is called acyclicgraph.
 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 complete graph is a graph if every node in a graph is connected to
every othernode.
 Two vertices are said to be connected if there is a path from vi to vj or vj to
vi
 connected graph: any two vertices are connected by some path. if there is a
path from vi to vj and vj to vi.
 A directed graph is said to be strongly connected if for every pair of distinct
vertices vi,vj in G, there is a path from vi to vj and also from vj to vi.
 Weakly connected graph- if a graph is not strongly connected but it is
connected
S. Durga Devi
,CSE,CBIT
A
D
B C
Acyclic graph
A
C
BD
Strongly Connected
A B
C D
E
CompleteGraph
Weighted Graph
A
D
B C
Cyclic graph
S. Durga Devi ,CSE,CBIT
S. Durga Devi ,CSE,CBIT
0 0
1 2 3
1 2 0
1 2
3
(i) ii) (iii) (iv)
(a) Some of the sub graph of G1
0
1 2
3
G1
0 0
1
0
1
2
(i) (ii) (iii)
(b) Some of the sub graph of G2
0
1
2
G2
S. Durga Devi
,CSE,CBIT
More definitions : Loop
An edge that connects the vertex with itself
A B
C
D
Or an edge whose starting and end vertices are same
S. Durga Devi ,CSE,CBIT
Even More Terminology
connected not connected
•connected graph: any two vertices are connected by some path
At least two vertices
are not connected in a
graph is called
disconnected graph.
S. Durga Devi ,CSE,CBIT
0 0
1 2 3
1 2 0
1 2
3(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
0 0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
0
1 2
3
G1
0
1
2
G3
Subgraphs Examples
S. Durga Devi ,CSE,CBIT
Simple graph
• Simple graph- A graph (digraph) if it does
not have any loops or parallel edges (edge
from v1 to v2 and v2 to v1) is called
simple graph.
S. Durga Devi ,CSE,CBIT
Representation of a Graph
There are two ways of representinga graph in memory:
SequentialRepresentation by means of Adjacency Matrix.
Linked Representation by means of Linked List.
S. Durga Devi ,CSE,CBIT
Adjacency Matrix
A B C D E
A 0 1 1 1 0
B 1 0 1 1 0
C 1 1 0 1 1
D 1 1 1 0 1
E 0 0 1 1 0
 Adjacency Matrix is a bit matrix which containsentries of only 0 and 1
 Since it stores the information of whether the adjacency vertices are present
or not.
 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.
A
B C
D E
S. Durga Devi ,CSE,CBIT
Linked List Representation
A
B
C
D
N E
B C D NULL
A B E NULLD
A C D NULL
A B E NULLC
C D NULL
A
B C
D E
 It saves the memory.
 The number of lists dependson the number of vertices in the graph.
 The header node in each list maintains a list of all adjacent vertices of a
node.
S. Durga Devi ,CSE,CBIT
Undirected Graph Adjacency List Adjacency Matrix
S. Durga Devi ,CSE,CBIT
Directed Graph Adjacency List Adjacency Matrix
S. Durga Devi ,CSE,CBIT
Comparison among various representations
• Adjacency matrix representation always requires n x n matrix
with n vertices, regardless of the number of edges. But from
the manipulation point of view, this representation is the best.
• Insertion, deletion and searching are concerned, in some cases,
linked list representation has an advantage, but overall
performance is consider, matrix representation of graph is
more powerful than all
S. Durga Devi ,CSE,CBIT
Graph Traversal Techniques
There are two standardgraph traversaltechniques:
Depth-FirstSearch (DFS)
Breadth-FirstSearch (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
visitof vertices.
 The order of visiting nodes depends on the node we have selected first
and hence the order of visitingnodes may not be unique.
S. Durga Devi ,CSE,CBIT
Depth-First Search
 DFS is similarto the in order traversal of a tree
 Stack is used to implement the Depth- First-Search.
 DFS follows the followingrules:
1. Select an unvisitednode X, visitit, and treat as the current
node
2. Find an unvisitedadjacentof the current node as Y, visitit,
and make it the new current node;
3. Repeat step 1 and 2 till there is a ‘dead end’. dead end means
a vertex which do not have adjacent nodes or no more nodes
can be visited.
4.aftercoming to a dead end we backtrackingalong to its parent
to see if it has another adjacent vertex other than Y and then
repeat the steps 1,2,3. until all vertices are visited.
S. Durga Devi ,CSE,CBIT
Depth-First Search
 A stack can be used to maintain the track of all paths from any
vertex so as to help backtracking.
 Initially starting vertex will be pushed onto the stack.
 To visit a vertex, we are to pop a vertex from the stack, and
then push all the adjacent vertices onto it.
 A list is maintained to store the vertices already visited.
 When a vertex is popped, whether it is already visited or not
that can be known by searching the list.
 If the vertex is already visited, we will simply ignore it and we
will pop the stack for the next vertex to be visited.
 This procedure will continue till the stack not empty
S. Durga Devi ,CSE,CBIT
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.
S. Durga Devi ,CSE,CBIT
The Depth First Search Tree Order : A, B, E, D, C
A
B
E
DC
A
B
E
DC
The Breadth First Search Tree Order : A, B, C, D, E
A
B
E
DC
Graph
S. Durga Devi ,CSE,CBIT
BFS Traversal Order
A B D E C G F H IA B C F E G D H I
DFS Traversal Order
A B C
D E F
G H I
A B C
D E F
G H I
S. Durga Devi ,CSE,CBIT
Practice problems
v1
v2
v6
v3
v7
v5
v8
v4
Dfs-
Bfs-
S. Durga Devi ,CSE,CBIT
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
S. Durga Devi ,CSE,CBIT
A spanningtree of a graph is just a sub graph that containsall the vertices and
is a tree( without any cycles in a graph)
A graph may have many spanning trees.
o
r
o
r
o
r
Some SpanningTrees from GraphAGraphA
Spanning Trees
S. Durga Devi ,CSE,CBIT
Minimum spanning tree
• Take an example that connect four cities in India like
vijayawada, hyderabad, banglore and mumbai by a graph
Hyderabad
Mumbai
Banglore vijayawada
270km
980km
580km
1000km
Find a short distance to cover all four cities
700km
650
S. Durga Devi ,CSE,CBIT
Minimum Spanning Trees
The minimum spanning tree for a given graph is the spanning tree of
minimum cost for that graph.
5
7
2
1
3
4
2
1
3
WeightedGraph Minimum Spanning Tree
Algorithms to find Minimum SpanningTrees are:
Kruskal‘s Algorithm
Prim‘s Algorithm
S. Durga Devi ,CSE,CBIT
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.
S. Durga Devi ,CSE,CBIT
C
FE
A B
D
5
64
3
4
2
1 2
3
2
C
FE
A B
D
3
2
1 2
2
Minimum Spanning Tree for the abovegraph is:
S. Durga Devi ,CSE,CBIT
Walk-Through
Consider an undirected, weight graph
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10
S. Durga Devi ,CSE,CBIT
Sort the edges by increasing edge weight
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Accepting edge (E,G) would create a cycle
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 
(B,F) 4 
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 
(B,F) 4 
(B,H) 4 
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 
(B,F) 4 
(B,H) 4 
(A,H) 5 
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G
2
3
3
3
edge dv
(B,E) 4 
(B,F) 4 
(B,H) 4 
(A,H) 5 
(D,F) 6
(A,B) 8
(A,F) 10
Done
Total Cost =  dv = 21
4
} not
considere
d
S. Durga Devi ,CSE,CBIT
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.
S. Durga Devi ,CSE,CBIT
Walk-Through
Initialize array
K dv pv
A F  
B F  
C F  
D F  
E F  
F F  
G F  
H F  
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Start with any node, say D
K dv pv
A
B
C
D T 0 
E
F
G
H
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A
B
C 3 D
D T 0 
E 25 D
F 18 D
G 2 D
H
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B
C 3 D
D T 0 
E 25 D
F 18 D
G T 2 D
H
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A
B
C 3 D
D T 0 
E 7 G
F 18 D
G T 2 D
H 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B
C T 3 D
D T 0 
E 7 G
F 18 D
G T 2 D
H 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A
B 4 C
C T 3 D
D T 0 
E 7 G
F 3 C
G T 2 D
H 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B 4 C
C T 3 D
D T 0 
E 7 G
F T 3 C
G T 2 D
H 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 
E 2 F
F T 3 C
G T 2 D
H 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
Table entries unchanged
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A 4 H
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A T 4 H
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A T 4 H
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Table entries unchanged
S. Durga Devi ,CSE,CBIT
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
S. Durga Devi ,CSE,CBIT
4
A
H
B
F
E
D
C
G
2
3
4
3
3
Cost of Minimum Spanning
Tree =  dv = 21
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Done
S. Durga Devi ,CSE,CBIT
C
FE
A B
D
3
2
1 2
2
C
FE
A B
D
5
64
3
4
2
1 2
3
2
Minimum Spanning Tree for the abovegraph is:
A B C D E F
A - 5 4 6 2 -
B 5 - - 2 - 3
C 4 - - - 3 -
D 6 2 - - 1 2
E 2 - 3 1 - 4
F - 3 - 2 4 -
Adjacency matrix
S. Durga Devi ,CSE,CBIT
1
3
4
65
2
6
3
6
5
5
1
6
4 2
5
Exercise:
S. Durga Devi ,CSE,CBIT

More Related Content

What's hot

Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Himanshu Choudhary
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.Education Front
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresAmrinder Arora
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search treeMayeesha Samiha
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11sumitbardhan
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREESiddhi Shrivas
 
Lecture 1 an introduction to data structure
Lecture 1   an introduction to data structureLecture 1   an introduction to data structure
Lecture 1 an introduction to data structureDharmendra Prasad
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10sumitbardhan
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of treeSiddhi Viradiya
 

What's hot (20)

Tree
TreeTree
Tree
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
Tree
TreeTree
Tree
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data Structures
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
Lecture 1 an introduction to data structure
Lecture 1   an introduction to data structureLecture 1   an introduction to data structure
Lecture 1 an introduction to data structure
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Dsu qb
Dsu qbDsu qb
Dsu qb
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Data Structures
Data StructuresData Structures
Data Structures
 
Tree
TreeTree
Tree
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 

Similar to Unit 4.2(graphs)

Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
graph.pptx
graph.pptxgraph.pptx
graph.pptxhijigaf
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfChristianKapsales1
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Abdul Naqashbandi
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptTalhaFarooqui12
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxzerihunnana
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - IAnirudh Raja
 

Similar to Unit 4.2(graphs) (20)

Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Chap 8 graph
Chap 8 graphChap 8 graph
Chap 8 graph
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graphs
GraphsGraphs
Graphs
 
Graphs
GraphsGraphs
Graphs
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
Graph
GraphGraph
Graph
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 
10.graph
10.graph10.graph
10.graph
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - I
 

Recently uploaded

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 

Recently uploaded (20)

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

Unit 4.2(graphs)

  • 1. Graphs: Introduction  Applications of graphs  Graph representations  graph traversals  Minimal Spanning Trees. S. Durga Devi ,CSE,CBIT
  • 2. Introduction to Graphs  Another non linear data structure.  In tree structure, there is a hierarchical relationship between parent and children, that is, one parent and many children.  On the other hand, in graph, the relationship is from many parents to many children. Definition-A graph G = (V,E) is composed of: V: set of vertices(or nodes) E: set of edges connecting the vertices in V • An edge e = (u,v) is a pair of vertices • Example: a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),(b,e), (c,d),(c,e),(d,e)} S. Durga Devi ,CSE,CBIT
  • 3. S. Durga Devi ,CSE,CBIT
  • 4. Graph terminology • Digraph- A digraph is also called a directed graph. It is a graph G, such that, G=<V,E>, where V is the set of all vertices and E is the set of ordered pair of elements from V. • Edges have directions v= A,B,C,D E=((A,B),(A,C),(A,D),(D,B)) A B C D A B C D These are directed graphs G directed if edges ordered pairs (u,v)  u v G undirected- if edges unordered pairs {u,v}   S. Durga Devi ,CSE,CBIT
  • 5. Adjacent vertices If two nodes are connected by an edge, they are neighbors (and the nodes are adjacentto each other).  The size of a graph is the number of nodes in it  The empty graph has size zero (no nodes). Loop-An edge that connects the vertex with itself Degree of a Vertex- the number of edges connected to that vertex • Fordirected graph, – the in-degree of a vertex v is the number of edges incident on to the vertex. – the out-degree of a vertex v is the number of edges emanating from that vertex. Examples 3 2 3 3 0 1 33 0 1 2 3 4 5 6 2 3 3 1 1 1 1 directed graph in-degree out-degree 0 1 2 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 S. Durga Devi ,CSE,CBIT
  • 6.  path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. a d e c b a b c d e a b e d c simple path: no vertex is repeated a b c d e b e c Cycle - some path with distinct vertices except that the last vertex is the same as the first vertex a c d a a b c d e Parallel edges- if there are one or More than one edges between the same pair of vertices. S. Durga Devi ,CSE,CBIT
  • 7.  A graph without cycles is called acyclicgraph.  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 complete graph is a graph if every node in a graph is connected to every othernode.  Two vertices are said to be connected if there is a path from vi to vj or vj to vi  connected graph: any two vertices are connected by some path. if there is a path from vi to vj and vj to vi.  A directed graph is said to be strongly connected if for every pair of distinct vertices vi,vj in G, there is a path from vi to vj and also from vj to vi.  Weakly connected graph- if a graph is not strongly connected but it is connected S. Durga Devi ,CSE,CBIT
  • 8. A D B C Acyclic graph A C BD Strongly Connected A B C D E CompleteGraph Weighted Graph A D B C Cyclic graph S. Durga Devi ,CSE,CBIT
  • 9. S. Durga Devi ,CSE,CBIT
  • 10. 0 0 1 2 3 1 2 0 1 2 3 (i) ii) (iii) (iv) (a) Some of the sub graph of G1 0 1 2 3 G1 0 0 1 0 1 2 (i) (ii) (iii) (b) Some of the sub graph of G2 0 1 2 G2 S. Durga Devi ,CSE,CBIT
  • 11. More definitions : Loop An edge that connects the vertex with itself A B C D Or an edge whose starting and end vertices are same S. Durga Devi ,CSE,CBIT
  • 12. Even More Terminology connected not connected •connected graph: any two vertices are connected by some path At least two vertices are not connected in a graph is called disconnected graph. S. Durga Devi ,CSE,CBIT
  • 13. 0 0 1 2 3 1 2 0 1 2 3(i) (ii) (iii) (iv) (a) Some of the subgraph of G1 0 0 1 0 1 2 0 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G3 0 1 2 3 G1 0 1 2 G3 Subgraphs Examples S. Durga Devi ,CSE,CBIT
  • 14. Simple graph • Simple graph- A graph (digraph) if it does not have any loops or parallel edges (edge from v1 to v2 and v2 to v1) is called simple graph. S. Durga Devi ,CSE,CBIT
  • 15. Representation of a Graph There are two ways of representinga graph in memory: SequentialRepresentation by means of Adjacency Matrix. Linked Representation by means of Linked List. S. Durga Devi ,CSE,CBIT
  • 16. Adjacency Matrix A B C D E A 0 1 1 1 0 B 1 0 1 1 0 C 1 1 0 1 1 D 1 1 1 0 1 E 0 0 1 1 0  Adjacency Matrix is a bit matrix which containsentries of only 0 and 1  Since it stores the information of whether the adjacency vertices are present or not.  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. A B C D E S. Durga Devi ,CSE,CBIT
  • 17. Linked List Representation A B C D N E B C D NULL A B E NULLD A C D NULL A B E NULLC C D NULL A B C D E  It saves the memory.  The number of lists dependson the number of vertices in the graph.  The header node in each list maintains a list of all adjacent vertices of a node. S. Durga Devi ,CSE,CBIT
  • 18. Undirected Graph Adjacency List Adjacency Matrix S. Durga Devi ,CSE,CBIT
  • 19. Directed Graph Adjacency List Adjacency Matrix S. Durga Devi ,CSE,CBIT
  • 20. Comparison among various representations • Adjacency matrix representation always requires n x n matrix with n vertices, regardless of the number of edges. But from the manipulation point of view, this representation is the best. • Insertion, deletion and searching are concerned, in some cases, linked list representation has an advantage, but overall performance is consider, matrix representation of graph is more powerful than all S. Durga Devi ,CSE,CBIT
  • 21. Graph Traversal Techniques There are two standardgraph traversaltechniques: Depth-FirstSearch (DFS) Breadth-FirstSearch (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 visitof vertices.  The order of visiting nodes depends on the node we have selected first and hence the order of visitingnodes may not be unique. S. Durga Devi ,CSE,CBIT
  • 22. Depth-First Search  DFS is similarto the in order traversal of a tree  Stack is used to implement the Depth- First-Search.  DFS follows the followingrules: 1. Select an unvisitednode X, visitit, and treat as the current node 2. Find an unvisitedadjacentof the current node as Y, visitit, and make it the new current node; 3. Repeat step 1 and 2 till there is a ‘dead end’. dead end means a vertex which do not have adjacent nodes or no more nodes can be visited. 4.aftercoming to a dead end we backtrackingalong to its parent to see if it has another adjacent vertex other than Y and then repeat the steps 1,2,3. until all vertices are visited. S. Durga Devi ,CSE,CBIT
  • 23. Depth-First Search  A stack can be used to maintain the track of all paths from any vertex so as to help backtracking.  Initially starting vertex will be pushed onto the stack.  To visit a vertex, we are to pop a vertex from the stack, and then push all the adjacent vertices onto it.  A list is maintained to store the vertices already visited.  When a vertex is popped, whether it is already visited or not that can be known by searching the list.  If the vertex is already visited, we will simply ignore it and we will pop the stack for the next vertex to be visited.  This procedure will continue till the stack not empty S. Durga Devi ,CSE,CBIT
  • 24. 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. S. Durga Devi ,CSE,CBIT
  • 25. The Depth First Search Tree Order : A, B, E, D, C A B E DC A B E DC The Breadth First Search Tree Order : A, B, C, D, E A B E DC Graph S. Durga Devi ,CSE,CBIT
  • 26. BFS Traversal Order A B D E C G F H IA B C F E G D H I DFS Traversal Order A B C D E F G H I A B C D E F G H I S. Durga Devi ,CSE,CBIT
  • 28. 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 S. Durga Devi ,CSE,CBIT
  • 29. A spanningtree of a graph is just a sub graph that containsall the vertices and is a tree( without any cycles in a graph) A graph may have many spanning trees. o r o r o r Some SpanningTrees from GraphAGraphA Spanning Trees S. Durga Devi ,CSE,CBIT
  • 30. Minimum spanning tree • Take an example that connect four cities in India like vijayawada, hyderabad, banglore and mumbai by a graph Hyderabad Mumbai Banglore vijayawada 270km 980km 580km 1000km Find a short distance to cover all four cities 700km 650 S. Durga Devi ,CSE,CBIT
  • 31. Minimum Spanning Trees The minimum spanning tree for a given graph is the spanning tree of minimum cost for that graph. 5 7 2 1 3 4 2 1 3 WeightedGraph Minimum Spanning Tree Algorithms to find Minimum SpanningTrees are: Kruskal‘s Algorithm Prim‘s Algorithm S. Durga Devi ,CSE,CBIT
  • 32. 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. S. Durga Devi ,CSE,CBIT
  • 33. C FE A B D 5 64 3 4 2 1 2 3 2 C FE A B D 3 2 1 2 2 Minimum Spanning Tree for the abovegraph is: S. Durga Devi ,CSE,CBIT
  • 34. Walk-Through Consider an undirected, weight graph 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 S. Durga Devi ,CSE,CBIT
  • 35. Sort the edges by increasing edge weight edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 36. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2 (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 37. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 38. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Accepting edge (E,G) would create a cycle S. Durga Devi ,CSE,CBIT
  • 39. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 40. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 41. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 42. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 43. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4  (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 44. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4  (B,F) 4  (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 45. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4  (B,F) 4  (B,H) 4  (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 46. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4  (B,F) 4  (B,H) 4  (A,H) 5  (D,F) 6 (A,B) 8 (A,F) 10 S. Durga Devi ,CSE,CBIT
  • 47. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 2 3 3 3 edge dv (B,E) 4  (B,F) 4  (B,H) 4  (A,H) 5  (D,F) 6 (A,B) 8 (A,F) 10 Done Total Cost =  dv = 21 4 } not considere d S. Durga Devi ,CSE,CBIT
  • 48. 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. S. Durga Devi ,CSE,CBIT
  • 49. Walk-Through Initialize array K dv pv A F   B F   C F   D F   E F   F F   G F   H F   4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 2 S. Durga Devi ,CSE,CBIT
  • 50. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Start with any node, say D K dv pv A B C D T 0  E F G H 2 S. Durga Devi ,CSE,CBIT
  • 51. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B C 3 D D T 0  E 25 D F 18 D G 2 D H 2 S. Durga Devi ,CSE,CBIT
  • 52. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B C 3 D D T 0  E 25 D F 18 D G T 2 D H 2 S. Durga Devi ,CSE,CBIT
  • 53. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B C 3 D D T 0  E 7 G F 18 D G T 2 D H 3 G 2 S. Durga Devi ,CSE,CBIT
  • 54. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B C T 3 D D T 0  E 7 G F 18 D G T 2 D H 3 G 2 S. Durga Devi ,CSE,CBIT
  • 55. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B 4 C C T 3 D D T 0  E 7 G F 3 C G T 2 D H 3 G 2 S. Durga Devi ,CSE,CBIT
  • 56. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B 4 C C T 3 D D T 0  E 7 G F T 3 C G T 2 D H 3 G 2 S. Durga Devi ,CSE,CBIT
  • 57. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 10 F B 4 C C T 3 D D T 0  E 2 F F T 3 C G T 2 D H 3 G 2 S. Durga Devi ,CSE,CBIT
  • 58. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A 10 F B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H 3 G 2 S. Durga Devi ,CSE,CBIT
  • 59. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 10 F B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H 3 G 2 Table entries unchanged S. Durga Devi ,CSE,CBIT
  • 60. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A 10 F B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2 S. Durga Devi ,CSE,CBIT
  • 61. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 4 H B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2 S. Durga Devi ,CSE,CBIT
  • 62. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A T 4 H B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2 S. Durga Devi ,CSE,CBIT
  • 63. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A T 4 H B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2 Table entries unchanged S. Durga Devi ,CSE,CBIT
  • 64. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A T 4 H B T 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2 S. Durga Devi ,CSE,CBIT
  • 65. 4 A H B F E D C G 2 3 4 3 3 Cost of Minimum Spanning Tree =  dv = 21 K dv pv A T 4 H B T 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2 Done S. Durga Devi ,CSE,CBIT
  • 66. C FE A B D 3 2 1 2 2 C FE A B D 5 64 3 4 2 1 2 3 2 Minimum Spanning Tree for the abovegraph is: A B C D E F A - 5 4 6 2 - B 5 - - 2 - 3 C 4 - - - 3 - D 6 2 - - 1 2 E 2 - 3 1 - 4 F - 3 - 2 4 - Adjacency matrix S. Durga Devi ,CSE,CBIT