0% found this document useful (0 votes)
25 views70 pages

2 1graph

The document covers key concepts in graph theory, including terminology, representation methods (adjacency matrix and list), and traversal techniques like BFS and DFS. It also discusses minimum spanning trees and algorithms for finding them, such as Prim's and Kruskal's, as well as hashing techniques and heap structures. Additionally, it highlights the applications of these concepts in various fields, including network design and optimization problems.

Uploaded by

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

2 1graph

The document covers key concepts in graph theory, including terminology, representation methods (adjacency matrix and list), and traversal techniques like BFS and DFS. It also discusses minimum spanning trees and algorithms for finding them, such as Prim's and Kruskal's, as well as hashing techniques and heap structures. Additionally, it highlights the applications of these concepts in various fields, including network design and optimization problems.

Uploaded by

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

SECTION-II

Graph, Hashing and Heap


Graphs:
Terminology and representation using Adjacency Matrix and
Adjacency Lists, Graph Traversals and Application: BFS and DFS.
Minimum Spanning tree: Prims and Kruskal’s Algorithm, Shortest Path
Algorithms: Single Source All destinations, all pair shortest path
algorithm, Topological Sort.

Hashing:
Hashing techniques, Hash table, Hash functions. Collision handling
and Collision resolution techniques, Cuckoo Hashing. Dynamic
Hashing: Motivation for Dynamic Hashing, Dynamic Hashing using
Directories, directory less Dynamic Hashing. Bloom Filters Bloom
Filter Design

Heap:
Amortized Analysis, Double Ended Priority queues, Leftist Trees,
Binomial Heaps, Fibonacci Heaps, skew heaps, pairing heaps.
Introduction
• A Graph is a non-linear data structure consisting of
vertices and edges. The vertices are sometimes also
referred to as nodes and the edges are lines or arcs
that connect any two nodes in the graph.
• Graph is composed of a set of vertices( V ) and a set of
edges( E ). The graph is denoted by G(V, E).
• Graph data structures are a powerful tool for
representing and analyzing complex relationships
between objects or entities. They are particularly
useful in fields such as social network analysis,
recommendation systems, and computer networks
Components of a Graph
• Vertices: Vertices are the
fundamental units of the
graph. Sometimes, vertices
are also known as vertex or
nodes. Every node/vertex can
be labeled or unlabelled.
• Edges: Edges are drawn or
used to connect two nodes of
the graph. It can be ordered
pair of nodes in a directed
graph. Edges can connect any
two nodes in any possible
way. There are no rules.
Sometimes, edges are also
known as arcs. Every edge
can be labelled/unlabelled.
Types Of Graph
1. Null Graph
A graph is known as a null
graph if there are no
edges in the graph.
2. Trivial Graph
Graph having only a single
vertex, it is also the
smallest graph possible.
Types Of Graph
3. Undirected Graph
A graph in which edges do
not have any direction.
That is the nodes are
unordered pairs in the
definition of every edge.
4. Directed Graph
A graph in which edge has
direction. That is the
nodes are ordered pairs in
the definition of every
edge.
Types Of Graph
5. Connected Graph
The graph in which from one
node we can visit any other
node in the graph is known as
a connected graph.
6. Disconnected Graph
The graph in which at least
one node is not reachable
from a node is known as a
disconnected graph.
Types Of Graph
7. Regular Graph
The graph in which the
degree of every vertex is
equal to K is called K
regular graph.
8. Complete Graph
The graph in which from
each node there is an
edge to each other node.
Types Of Graph
9. Cycle Graph
The graph in which the
graph is a cycle in itself, the
degree of each vertex is 2.
10. Cyclic Graph
A graph containing at least
one cycle is known as a
Cyclic graph.
Types Of Graph
11. Directed Acyclic Graph
A Directed Graph that
does not contain any
cycle.
12. Bipartite Graph
A graph in which vertex
can be divided into two
sets such that vertex in
each set does not contain
any edge between them.
Types Of Graph
13. Weighted Graph
• A graph in which the edges are already
specified with suitable weight is known as a
weighted graph.
• Weighted graphs can be further classified as
directed weighted graphs and undirected
weighted graphs.
Tree v/s Graph
• Trees are the restricted types of graphs. Every tree will always
be a graph but not all graphs will be trees. Linked List, Trees,
and Heaps all are special cases of graphs.
Representation of Graphs
• There are two ways to store a graph:
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix
• In this method, the graph is stored in the form of
the 2D matrix where rows and columns denote
vertices. Each entry in the matrix represents the
weight of the edge between those vertices.
Adjacency List
• This graph is represented as a collection of
linked lists. There is an array of pointer which
points to the edges connected to that vertex.
Comparison between Adjacency
Matrix and Adjacency List
• When the graph contains a large number of edges
then it is good to store it as a matrix because only
some entries in the matrix will be empty.
• An algorithm such as Prim and Dijkstra uses adjacency
matrix to have less complexity.
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
• There are two graph traversal techniques and
they are as follows...
1. DFS (Depth First Search)
2. BFS (Breadth First Search)
DFS (Depth First Search)
• DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops.
• Stack data structure with maximum size as total number of vertices in the graph is used.
Steps for DFS traversal:
Step 1 - Define a Stack of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it .
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack
and push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top
of the stack.
Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex .
Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges
from the graph
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
BFS (Breadth First Search)
• BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a
graph without loops.
• Queue data structure with maximum size of total number of vertices in the graph
is used to implement BFS traversal.
Steps for BFS traversal:
Step 1 - Define a Queue of size total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
Step 4 - When there is no new vertex to be visited from the vertex which is at front of
the Queue then delete that vertex.
Step 5 - Repeat steps 3 and 4 until queue becomes empty.
Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS vs DFS
BFS DFS

Full form BFS stands for Breadth First DFS stands for Depth First Search.
Search.
Technique It a vertex-based technique It is an edge-based technique
to find the shortest path in a because the vertices along the
graph. edge are explored first from the
starting to the end node.

Definition BFS is a traversal technique DFS is also a traversal technique in


in which all the nodes of the which traversal is started from the
same level are explored root node and explore the nodes
first, and then we move to as far as possible until we reach
the next level. the node that has no unvisited
adjacent nodes.
BFS vs DFS
Data Structure Queue data structure is used Stack data structure is used for
for the BFS traversal. the BFS traversal.
Backtracking BFS does not use the DFS uses backtracking to
backtracking concept. traverse all the unvisited
nodes.
Number of BFS finds the shortest path In DFS, a greater number of
edges having a minimum number of edges are required to traverse
edges to traverse from the from the source vertex to the
source to the destination destination vertex.
vertex.
Optimality BFS traversal is optimal for DFS traversal is optimal for
those vertices which are to be those graphs in which
searched closer to the source solutions are away from the
vertex. source vertex.
BFS vs DFS

Speed BFS is slower than DFS. DFS is faster than BFS.


Suitability for It is not suitable for the It is suitable for the
decision tree decision tree because it decision tree. Based on
requires exploring all the the decision, it explores
neighboring nodes first. all the paths. When the
goal is found, it stops its
traversal.
Memory It is not memory efficient It is memory efficient as
efficient as it requires more it requires less memory
memory than DFS. than BFS
BFS Algorithm Applications
• To build index by search index
• For GPS navigation
• Path finding algorithms
• In Ford-Fulkerson algorithm to find maximum
flow in a network
• Cycle detection in an undirected graph
• In minimum spanning tree
DFS Algorithm Applications
• For finding the path
• To test if the graph is bipartite
• For finding the strongly connected
components of a graph
• For detecting cycles in a graph
Spanning tree
• A Spanning tree can be defined as a subset of a
graph, which consists of all the vertices covering
minimum possible edges and does not have a
cycle. Spanning tree cannot be disconnected.
• Every connected and undirected graph has at least
one spanning tree. A disconnected graph does not
have a spanning tree as it is not possible to
include all vertices.
Spanning tree
In general, if N is the
number of nodes in a
graph, then a
complete connected
graph has maximum
NN-2 number of
spanning trees. Thus
in the above graph N
=3, therefore, it has
3(3-2) = 3 spanning
trees.
Properties of the spanning tree
• A connected graph can have more than one spanning trees.
• All spanning trees in a graph have the same number of
nodes and edges.
• If we remove one edge from the spanning tree, then it will
become minimally connected and will make the graph
disconnected.
• On the other hand, adding one edge to the spanning tree
will make it maximally acyclic thereby creating a loop.
• A spanning tree does not have a loop or a cycle.
Minimum Spanning Tree (MST)
• A minimum spanning tree is the one that contains the
least weight among all the other spanning trees of a
connected weighted graph.
• There can be more than one minimum spanning tree
for a graph.
• There are two most popular algorithms that are used
to find the minimum spanning tree in a graph.
1. Kruskal’s algorithm
2. Prim’s algorithm
Kruskal’s Algorithm
• Kruskal’s algorithm is an algorithm to find the MST in a connected
graph.
• Kruskal’s algorithm finds a subset of a graph G such that:
– It forms a tree with every vertex in it.
– The sum of the weights is the minimum among all the spanning trees
that can be formed from this graph.
• The sequence of steps for Kruskal’s algorithm is given as follows:
– First sort all the edges from the lowest weight to highest.
– Take edge with the lowest weight and add it to the spanning tree. If the
cycle is created, discard the edge.
– Keep adding edges like in step 1 until all the vertices are considered.
Kruskal’s Algorithm
Step 1: Select the edge with the least
weight which is 2-4.

Step 2: Select the next Step 3: Select next edge with the shortest
shortest edge 2-3 edge and that does not create a cycle i.e. 0-3
Kruskal’s Algorithm
Step 4: Select the shortest edge so that it doesn’t form a cycle. This is 0-1.
Prim’s Algorithm
• Prim’s algorithm is yet another algorithm to find the minimum
spanning the tree of a graph. In contrast to Kruskal’s algorithm that
starts with graph edges, Prim’s algorithm starts with a vertex. We
start with one vertex and keep on adding edges with the least
weight till all the vertices are covered.
• The sequence of steps for Prim’s Algorithm is as follows:
– Choose a random vertex as starting vertex and initialize a
minimum spanning tree.
– Find the edges that connect to other vertices. Find the edge with
minimum weight and add it to the spanning tree.
– Repeat step 2 until the spanning tree is obtained.
Prim’s Algorithm
Step 1: Select node 2 as the random
vertex.

Step 2: Select the edge with the Step 3: Select another vertex that is not in the
least weight from 2. We choose spanning tree yet. We choose the edge 2-3.
edge 2-4
Prim’s Algorithm
Step 4: Select an edge with least weight Step 5: Select an edge with the least weight
from the above vertices. We have edge 3-0 from vertex 0. This is the edge 0-1.
which has the least weight. Covered all the vertices in the graph and
obtained a complete spanning tree with
minimum cost.
Prim’s vs. Kruskal’s algorithm
Prim’s Algorithm Kruskal’s Algorithm

It starts to build the


It starts to build the Minimum Minimum Spanning Tree
Spanning Tree from any vertex in from the vertex carrying
the graph. minimum weight in the
graph.

It traverses one node more than


It traverses one node only
one time to get the minimum
once.
distance.

Prim’s algorithm has a time


Kruskal’s algorithm’s time
complexity of O(V2), V being the
complexity is O(E log V), V
number of vertices and can be
being the number of
improved up to O(E log V) using
Prim’s vs. Kruskal’s algorithm
Kruskal’s algorithm can
Prim’s algorithm gives
generate forest(disconnected
connected component as
components) at any instant as
well as it works only on
well as it can work on
connected graph.
disconnected components

Prim’s algorithm runs Kruskal’s algorithm runs faster


faster in dense graphs. in sparse graphs.

It generates the minimum It generates the minimum


spanning tree starting from spanning tree starting from the
the root vertex. least weighted edge.
Prim’s vs. Kruskal’s algorithm

Applications of prim’s
algorithm are Travelling Applications of Kruskal
Salesman Problem, Network algorithm are LAN
for roads and Rail tracks connection, TV Network etc.
connecting all the cities etc.

Prim’s algorithm prefer list Kruskal’s algorithm prefer


data structures. heap data structures.
Example
Applications Of Spanning Tree
Communications Network Setup: When we want to set up a communication
network using communication links, then the cost of setting up communication
links between two points is best determined using an MST.
Cluster Analysis: It can be used to solve the K-clustering problem by finding a
minimum spanning tree and deleting the k-1 most expensive edges.
Laying Out Road/Rail Networks: When we lay various road or rail networks
between or within cities, the cost of the project is a very important factor. We can
find the best path with minimum cost using minimum spanning trees.
Planning Housing Facilities: Facilities like electricity, water, sewage, etc. to be
provided to a number of houses also require to be at optimum cost and this is
done using an MST.
Solving the Travelling Salesman Problem: We can use an MST to solve the
traveling salesman problem which requires to visit each point at least once.
• Shortest Path Algorithms: Single Source All
destinations, all pair shortest path algorithm,
Topological Sort.
Shortest Path Algorithms
• The shortest path problem is about finding a
path between 2 vertices in a graph such that
the total sum of the edges weights is
minimum.
• This problem could be solved easily
using (BFS) if all edge weights were (1), but
here weights can take any value.
Shortest Path Algorithms
• Three different algorithms on the use-case
1. Bellman Ford's Algorithm- Bellman Ford's algorithm
is used to find the shortest paths from the source
vertex to all other vertices in a weighted graph.
2. Dijkstra's Algorithm- Dijkstra's algorithm has many
variants but the most common one is to find the
shortest paths from the source vertex to all other
vertices in the graph
3. Floyd Warshall's Algorithm- Floyd or Warshall's
Algorithm is used to find the shortest paths between
between all pairs of vertices in a graph
Bellman Ford's Algorithm
Algorithm Steps:
• The outer loop traverses from 0 : n−1.
• Loop over all edges, check if the next node
distance > current node distance + edge
weight, in this case update the next node
distance to "current node distance + edge
weight".
Dijkstra's Algorithm-
Algorithm Steps:
• Mark the source node with a current distance of 0 and
the rest with infinity.
• Set the non-visited node with the smallest current
distance as the current node.
• For each neighbor, N of the current node adds the
current distance of the adjacent node with the weight
of the edge connecting 0->1. If it is smaller than the
current distance of Node, set it as the new current
distance of N.
• Mark the current node 1 as visited.
• Go to step 2 if there are any nodes are unvisited
Dijkstra's Algorithm-
Dijkstra's Algorithm-
Floyd Warshall's Algorithm-
Floyd Warshall's Algorithm-
Floyd Warshall's Algorithm-
Floyd Warshall's Algorithm-
Floyd Warshall's Algorithm-
Floyd Warshall's Algorithm-
Topological Sorting
• Topological sorting for Directed Acyclic Graph
(DAG) is a linear ordering of vertices such that
for every directed edge u v, vertex u comes
before v in the ordering.
• Note: Topological Sorting for a graph is not
possible if the graph is not a DAG.
Topological Sorting
• For example, a topological
sorting of the following graph is
“5 4 2 3 1 0”. There can be more
than one topological sorting for
a graph. Another topological
sorting of the following graph is
“4 5 2 3 1 0”. The first vertex in
topological sorting is always a
vertex with an in-degree of 0 (a
vertex with no incoming edges).
Algorithm for Topological Sorting
• Create a stack to store the nodes.
• Initialize visited array of size N to keep the record of visited nodes.
• Run a loop from 0 till N
– if the node is not marked True in visited array
• Call the recursive function for topological sort and perform the following steps.
– Mark the current node as True in the visited array.
– Run a loop on all the nodes which has a directed edge to the current node
» if the node is not marked True in the visited array:
• Recursively call the topological sort function on the node
– Push the current node in the stack.

• Print all the elements in the stack


Algorithm for Topological Sorting

You might also like