Data structures (Graph) (1)
Data structures (Graph) (1)
Data structures (Graph) (1)
Instructor:
Ms.Dur-e-Shawar Agha
ROAD MAP
Introduction to Graph
Types of Graph
Dijkstra's algorithm
Prims algorithm
Graph
A graph is a pictorial representation of a set of objects where some pairs of objects are connected by
links. The interconnected objects are represented by points termed as vertices, and the links that
connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges,
connecting the pairs of vertices. Take a look at the following graph.
• In Facebook, users are considered to be the vertices and if they are friends then there
is an edge running between them
• In Operating System, we come across the Resource Allocation Graph where each
process and resources are considered to be vertices.
Types of graphs
Weighted Graph
Unweighted Graph
Undirected Graph
Directed Graph
Complete Graph
Weighted graphs
• All the values seen associated with the edges are called
weights.
• By default, all the graphs are unweighted unless there is a value associated.
Directed graph
• Directed graph also called a digraph, where a set of objects (V E) are connected
• Where a set of objects are connected, and all the edges are bidirectional.
The two most commonly used data structures to store graphs are:
1. Adjacency List
2. Adjacency Matrix
1. Adjacency List
• The DFS search begins starting from the first node and goes deeper and deeper,
exploring down until the targeted node is found.
• If the targeted key is not found, the search path is changed to the path that was
stopped exploring during the initial search, and the same procedure is repeated for that
branch.
• We use Stack data structure with maximum size of total number of vertices in the
graph to implement DFS traversal.
Contd….
• We use Queue data structure with maximum size of total number of vertices in the
graph to implement BFS traversal.
Contd….
A B C
D
BFS(Breadth First Search)
DFS(Breadth First Search)
Dijkstra’s Algorithm
It is a solution to the single-source shortest path problem in graph theory.
Works on both directed and undirected graphs. However, all edges must have
nonnegative weights.
Approach: Greedy
Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge
weights are nonnegative
Output: Lengths of shortest paths (or the shortest paths themselves) from a
given source vertex v∈V to all other vertices
Pseudocode
For sparse graphs, or graphs with very few edges and many nodes, it can be
implemented more efficiently storing the graph in an adjacency list using a
binary heap or priority queue. This will produce a running time of
It is used for finding the Minimum Spanning Tree (MST) of a given graph.
To apply Prim’s algorithm, the given graph must be weighted, connected and
undirected.
Prim’s Algorithm Implementation
Step-01:
The vertex connecting to the edge having least weight is usually selected.
Step-02:
Find all the edges that connect the tree to new vertices.
Find the least weight edge among those edges and include it in the existing tree.
If including that edge creates a cycle, then reject that edge and look for the next
least weight edge.
Step-03:
Keep repeating step-02 until all the vertices are included and Minimum
Spanning Tree (MST) is obtained.
PRACTICE PROBLEMS BASED ON PRIM’S ALGORITHM
Construct the minimum spanning tree (MST) for the given graph using
Prim’s Algorithm.
The above discussed steps are followed to find the minimum cost spanning
tree using Prim’s Algorithm
Step-01:
Since all the vertices have been included in the MST, so we stop.
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
Activity#01
Types of Graph
Graph Traversals