UNIT-4
UNIT-4
Graph
Graph is a non-linear data structure. It contains a set of
points known as nodes (or vertices) and a set of links
known as edges (or Arcs). Here edges are used to
connect the vertices
Graph is a collection of nodes and edges in which nodes
are connected with edges.
Example
1) Vertex
Individual data element of a graph is called as Vertex.
Vertex is also known as node.
2) Edge
• An edge is a connecting link between two vertices.
Edge is also known as Arc. An edge is represented as
(startingVertex, endingVertex).
Types of Edges
• Undirected Edge - An undirected edge is a
bidirectional edge. If there is undirected edge
between vertices A and B then edge (A , B) is equal to
edge (B , A).
• Directed Edge - A directed edge is a unidirectional
edge. If there is directed edge between vertices A
and B then edge (A , B) is not equal to edge (B , A).
• Weighted Edge - A weighted edge is a edge with
value (cost) on it.
Undirected Graph
• A graph with only undirected edges is said to be
undirected graph.
Directed Graph
• A graph with only directed edges is said to be
directed graph.
Mixed Graph
• A graph with both undirected and directed edges is
said to be mixed graph
• Self-loop
• Edge (undirected or directed) is a self-loop if its two
endpoints coincide with each other.
• Simple Graph
• A graph is said to be simple if there are no parallel
and self-loop edges.
• Path
• A path is a sequence of alternate vertices and edges
that starts at a vertex and ends at other vertex such
that each edge is incident to its predecessor and
successor vertex.
Graph Representations
4 (5,4) 22
7 (5,7) 24
PRIMS Algorithm Example
VERTEX EDGE WEIGHT
2 (1,2) 28
7 (5,7),(4,7) 24,18
3 (4,3) 12
PRIMS Algorithm Example
VERTEX EDGE WEIGHT
2 (1,2), (3,2) 28,16
7 (5,7),(4,7) 24,18
PRIMS Algorithm Example
VERTEX EDGE WEIGHT
7 (5,7),(4,7),(2,7) 24,18,14
PRIMS Algorithm Example
Since all the vertices have been included in the MST, so
we stop.
Weight of the MST
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
PRIMS Algorithm Example-2
Construct the minimum spanning tree (MST) for the
given graph using Prim’s Algorithm-
PRIMS Algorithm Example-2
• Solution:-
PRIMS Algorithm Example-2
Solution:-
Weight of the MST
= Sum of all edge weights
= 1 + 4 + 2 + 6 + 3 + 10
= 26 units
Graph Traversal
Graph traversal is a technique used for a searching
vertex in a graph.
The graph traversal is also used to decide the order of
vertices is visited in the search process.
A graph traversal finds the edges to be used in the
search process without creating loops. That means
using graph traversal we visit all the vertices of the
graph without getting into looping path.
Graph traversal techniques
• DFS (Depth First Search)
• BFS (Breadth First Search)
DFS (Depth First Search)