2 1graph
2 1graph
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.
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
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.