Download as pdf or txt
Download as pdf or txt
You are on page 1of 56

GRAPH

Introduction
• A graph is a non-linear kind of data structure made up of nodes or vertices and
edges.
• The edges connect any two nodes in the graph, and the nodes are also known as
vertices.

• This graph has a set of vertices V= { 1,2,3,4,5} and a set of edges E= {


(1,2),(1,3),(2,3),(2,4),(2,5),(3,5),(4,50 }.
Types Of Graph
• Weighted Graph :
• A graph G= (V, E) is called a labeled or weighted graph because each edge
has a value or weight representing the cost of traversing that edge.

• Directed Graph :
• A directed graph also referred to as a digraph, is a set of nodes connected
by edges, each with a direction.
Types Of Graph
• Disconnected Graph:
• When there is no edge linking the vertices, you refer to the null
graph as a disconnected graph.

• Cyclic Graph :
• If a graph contains at least one graph cycle, it is considered to be
cyclic.
Types Of Graph
Acyclic Graph :
• When there are no cycles in a graph, it is called an acyclic graph.

• Directed Acyclic Graph :


• It's also known as a directed acyclic graph (DAG), and it's a graph
with directed edges but no cycle. It represents the edges using an
ordered pair of vertices since it directs the vertices and stores some
data.
Types Of Graph
Sub graph :
• The vertices and edges of a graph that are subsets of another graph
are known as a sub graph.
Terminologies of Graphs
• An edge is one of the two primary units used to form graphs. Each
edge has two ends, which are vertices to which it is attached.
• If two vertices are endpoints of the same edge, they are adjacent.
• A vertex's outgoing edges are directed edges that point to the origin.
• A vertex's incoming edges are directed edges that point to the
vertex's destination.
• The total number of edges occurring to a vertex in a graph is its
degree.
• The out-degree of a vertex in a directed graph is the total number of
outgoing edges, whereas the in-degree is the total number of
incoming edges.
• A vertex with an in-degree of zero is referred to as a source vertex,
while one with an out-degree of zero is known as sink vertex.
• An isolated vertex is a zero-degree vertex that is not an edge's
endpoint.
• A path is a set of alternating vertices and edges, with each vertex
connected by an edge.
Terminologies of Graphs
• The path that starts and finishes at the same vertex is known as a
cycle.
• A path with unique vertices is called a simple path.
• For each pair of vertices x, y, a graph is strongly connected if it
contains a directed path from x to y and a directed path from y to x.
• A directed graph is weakly connected if all of its directed edges are
replaced with undirected edges, resulting in a connected graph. A
weakly linked graph's vertices have at least one out-degree or in-
degree.
• A tree is a connected forest. The primary form of the tree is called a
rooted tree, which is a free tree.
• A spanning sub graph that is also a tree is known as a spanning tree.
• A connected component is the unconnected graph's most connected
sub graph.
• A bridge, which is an edge of removal, would sever the graph.
• Forest is a graph without a cycle.
Representation of Graphs
• The most frequent graph representations are the two that
follow:
• Adjacency matrix
• Adjacency list
Adjacency Matrix
• A sequential representation is an adjacency matrix.
• It's used to show which nodes are next to one another.
I.e., is there any connection between nodes in a graph?
• You create an MXM matrix G for this representation.
If an edge exists between vertex a and vertex b, the corresponding
element of G, gi,j = 1, otherwise gi,j = 0.
• If there is a weighted graph, you can record the edge's weight
instead of 1s and 0s.
Representation of Graphs
Undirected Graph Representation

Directed Graph Representation


Representation of Graphs
• Weighted Undirected Graph Representation
• Weight or cost is indicated at the graph's edge, a weighted graph
representing these values in the matrix.
Representation of Graphs
• Adjacency List :
• A linked representation is an adjacency list.
• You keep a list of neighbors for each vertex in the graph in this
representation. It means that each vertex in the graph has a list of
its neighboring vertices.
• You have an of vertices indexed by the vertex number, and the
corresponding array member for each vertex x points to a singly
linked list of x's neighbors.
• Weighted Undirected Graph Representation Using Linked-
List
Representation of Graphs
• Weighted Undirected Graph Representation Using an
Array
Operations on Graphs
• Creating graphs
• Insert vertex
• Delete vertex
• Insert edge
• Delete edge
Operations on Graphs
• Creating graphs
• Adjacency Matrix :
• The adjacency matrix of a simple labeled graph, also known as the
connection matrix, is a matrix with rows and columns labeled by
graph vertices and a 1 or 0 in position depending on whether they
are adjacent or not.
• Adjacency List :
• A finite graph is represented by an adjacency list, which is a
collection of unordered lists.
• Each unordered list describes the set of neighbors of a particular
vertex in the graph within an adjacency list.
Operations on Graphs
• Insert Vertex
• When you add a vertex that after introducing one or more vertices
or nodes, the graph's size grows by one, increasing the matrix's size
by one at the row and column levels.
Operations on Graphs
• Delete Vertex
• Deleting a vertex refers to removing a specific node or vertex from a
graph that has been saved.
• If a removed node appears in the graph, the matrix returns that
node. If a deleted node does not appear in the graph, the matrix
returns the node not available.
Operations on Graphs
• Insert Edge
• Connecting two provided vertices can be used to add an edge to a
graph.
Operations on Graphs
• Insert Edge
• Connecting two provided vertices can be used to add an edge to a
graph.
Operations on Graphs
• Delete Edge
• The connection between the vertices or nodes can be removed to
delete an edge.
Operations on Graphs
• Delete Edge
• The connection between the vertices or nodes can be removed to
delete an edge.
Graph Traversal Algorithm
• There are two techniques to implement a graph traversal
algorithm:
• Breadth-first search
• Depth-first search
• Breadth-First Search or BFS
• It begins at the root of the graph and investigates all nodes at the
current depth level before moving on to nodes at the next depth
level.
• To maintain track of the child nodes that have been encountered but
not yet inspected, more memory, generally you require a queue.
Graph Traversal Algorithm
• Algorithm Breadth-First Search

• Step 1: Consider the graph you want to navigate.


• Step 2: Select any vertex in your graph, say v1, from which you want to
traverse the graph.
• Step 3: Examine any two data structures for traversing the graph.
• Visited array (size of the graph) Queue data structure
• Step 4: Starting from the vertex, you will add to the visited array, and
afterward, you will v1's adjacent vertices to the queue data structure.
• Step 5: Now, using the FIFO concept, you must remove the element from
the queue, put it into the visited array, and then return to the queue to add
the adjacent vertices of the removed element.
• Step 6: Repeat step 5 until the queue is not empty and no vertex is left to
be visited.
Graph Traversal Algorithm
Graph Traversal Algorithm
• Depth-First Search or DFS
• The depth-first search (DFS) algorithm traverses or explores data
structures such as trees and graphs.
• The DFS algorithm begins at the root node and examines each
branch as far as feasible before backtracking.
• To maintain track of the child nodes that have been encountered but
not yet inspected, more memory, generally a stack, is required.
Graph Traversal Algorithm
• Algorithm of depth-first search

• Step 1: Consider the graph you want to navigate.


• Step 2: Select any vertex in our graph, say v1, from which you want
to begin traversing the graph.
• Step 3: Examine any two data structures for traversing the graph.
Visited array (size of the graph) Stack data structure
• Step 4: Insert v1 into the array's first block and push all the adjacent
nodes or vertices of vertex v1 into the stack.
• Step 5: Now, using the FIFO principle, pop the topmost element and
put it into the visited array, pushing all of the popped element's nearby
nodes into it.
• Step 6: If the topmost element of the stack is already present in the
array, discard it instead of inserting it into the visited array.
• Step 7: Repeat step 6 until the stack data structure isn't empty.
Graph Traversal Algorithm
Spanning Tree
• If you have graph G with vertices V and edges E, then that
graph can be represented as G(V, E).
• For this graph G(V, E), if you construct a tree structure G’(V’,
E’) such that the formed tree structure follows constraints
mentioned below, then that structure can be called a Spanning
Tree.
1. V’ = V (number of Vertices in G’ must be equal to the number
of vertices in G)
2. E’ = |V| - 1 (Edges of G’ must be equal to the number of
vertices in graph G minus 1)
Creating Spanning Trees for Given Graph
Spanning Tree
• Additionally, the number of edges E’ must be equal to the
number of vertices in the graph minus one, i.e., 4. For this given
graph, five spanning trees can be constructed as shown below:
How to Calculate the Number of
Possible Spanning Trees
• A connected graph can have several spanning trees, as previously stated. So,
how do you estimate how many distinct spanning trees can be created for a
given graph?
• The number of possible spanning trees for this complete graph can be
calculated using Cayley’s Formula
• n(ST)complete graph =V(v-2)
• The graph given below is an example of a complete graph consisting of 4
vertices and 6 edges.
• For this graph, number of possible spanning trees will be:
Properties of Spanning Tree
• A spanning tree whose overall resultant weight value is minimal is
considered to be a Minimal Spanning Tree.
• A connected graph can have more than one spanning tree.
• All Spanning trees must contain the same number of vertices as of
graph, and the number of edges must be equal to |V| - 1.
• The spanning tree must not contain any cycle.
• If the given graph is a cycle graph, then the number of possible
spanning trees will be equal to the number of vertices of the given
graph.
• If the given graph is a complete graph, then the number of possible
spanning trees can be calculated using Cayley’s Formula.
Properties of Spanning Tree
• A spanning tree cannot be disconnected. If you remove any edge from
the created tree, then it won’t be considered as a spanning tree
anymore. It can only be regarded as a disconnected graph.

• There is a chance that there will be more than one minimum spanning
tree if there are numerous edges with the same weight.
Application of Spanning Tree
• Telecommunication Network Building:
Application of Spanning Tree
• Constructing Highways or Railroads :
• Basically, the algorithm treats the cities as the vertices and paths
joining them as edges to create a subtree that will make the route fully
connected and cost efficient.
Application of Spanning Tree
• Image Segmentation:
Dijkstra’s Algorithm
• Edsger W.Dijkstra invented Dijkstra’s algorithm to find the shortest
path for the required node between multiple nodes in the graph to
design a shortest-path tree.
• Dijkstra’s algorithm is used to find the shortest path between the two
mentioned vertices of a graph by applying the Greedy Algorithm as
the basis of principle.
• For Example: Used to find the shortest between the destination to
visit from your current location on a Google map.
Dijkstra’s Algorithm

• 1. We will find the shortest path from node A to the other nodes in the graph,
assuming that node A is the source.
• 2. For node B, apply the above-discussed algorithm, then on applying values:
if 0 + 20 < ∞
>[TRUE]
Node A to Node B = 20
• 3. For node C, on applying the approach from node A, we have:
if 0 + 50 < ∞
>[TRUE]
Node A to Node B = 50
• 4. For node C, from node B, on applying the algorithm, we have:
if 20 + 10 < ∞
>[TRUE]
Node B to Node C = 30
• 5. By the value obtained from step 3, we change the shortest distance between
node A to Node C to 30 from the previous distance of 50.
Dijkstra’s Example
Dijkstra’s solution
1. All the distances from node A to the rest of the nodes is ∞.
2. Calculating the distance between node A and the immediate nodes
(node B & node D).
3. Choose the node with the shortest distance to be the current node
from unvisited nodes, i.e., node B. Calculating the distance between
node B and the immediate nodes.
4. Choose the node with the shortest distance to be the current node
from unvisited nodes, i.e., node D. Calculating the distance between
node D and the immediate nodes.
5. Choose the node with the shortest distance to be the current node
from unvisited nodes, i.e., node E. Calculating the distance between
node E and the immediate nodes.
6. Choose the node with the shortest distance to be the current node
from unvisited nodes, i.e., node F. Calculating the distance between
node F and the immediate nodes.
What Is the Bellman-Ford Algorithm?
• Dynamic Programming is used in the Bellman-Ford algorithm.
• It begins with a starting vertex and calculates the distances between
other vertices that a single edge can reach.
• It then searches for a path with two edges, and so on. The Bellman-
Ford algorithm uses the bottom-up approach.
• Based on the "Principle of Relaxation," more accurate values
gradually recovered an approximation to the proper distance until
finally reaching the optimum solution.
• Negative weight edges can generate negative weight cycles, which
reduce the total path distance by returning to the same point.
What Is the Bellman-Ford Algorithm?
• Step 1: Make a list of all the graph's edges. This is simple if an
adjacency list represents the graph.
• Step 2: "V - 1" is used to calculate the number of iterations. Because
the shortest distance to an edge can be adjusted V - 1 time at most,
the number of iterations will increase the same number of vertices.
• Step 3: Begin with an arbitrary vertex and a minimum distance of
zero. Because you are exaggerating the actual distances, all other
nodes should be assigned infinity.
For each edge u-v, relax the path lengths for the vertices:
If distance[v] is greater than distance[u] + edge weight uv, then
distance[v] = distance[u] + edge weight uv
• Step 4:If the new distance is less than the previous one, update the
distance for each Edge in each iteration. The distance to each node
is the total distance from the starting node to this specific node.
• Step 5: To ensure that all possible paths are considered, you must
consider alliterations. You will end up with the shortest distance if
you do this.
What Is the Bellman-Ford Algorithm?
What Is the Bellman-Ford Algorithm?
What Is the Bellman-Ford Algorithm?
What Is the Bellman-Ford Algorithm?
What Is the Bellman-Ford Algorithm?
Prim’s Algorithm
• Prim’s algorithm is used to find the Minimum Spanning Tree for a
given graph.
• Prim's approach identifies the subset of edges that includes every
vertex in the graph, and allows the sum of the edge weights to be
minimized.
• Prim’s algorithm starts with a single node and works its way
through several adjacent nodes, exploring all of the connected edges
along the way.
• Edges with the minimum weights that do not cause cycles in the
graph get selected for t inclusion in the MST structure. That is why it
is also known as a Greedy Algorithm.
How to Create MST Using Prim’s Algorithm
• Step 1: Determine the arbitrary starting vertex.
• Step 2: Keep repeating steps 3 and 4 until the fringe vertices (vertices
not included in MST)remain.
• Step 3: Select an edge connecting the tree vertex and fringe vertex
having the minimum weight.
• Step 4: Add the chosen edge to MST if it doesn’t form any closed cycle.
• Step 5: Exit

• Graph G(V, E) given below contains 9 vertices and 12 edges. We are


supposed to create a minimum spanning tree T(V’, E’) for G(V, E) such
that the number of vertices in T will be 9 and edges will be 8 (9-1).
How to Create MST Using Prim’s Algorithm
How to Create MST Using Prim’s Algorithm
• Primarily, to begin with the creation of MST, you will choose an
arbitrary starting vertex. Let’s say node A is your starting vertex. This
means it will be included first in your tree structure.

• After the inclusion of node A, you will look into the connected edges
going outward from node A and you will pick the one with a minimum
edge weight to include it in your T(V’, E’) structure.
How to Create MST Using Prim’s Algorithm
• Now, you have reached node B. From node B, there are two possible
edges out of which edge BD has the least edge weight value. So, you will
include it in your MST.

• From node D, you only have one edge. So, you will include it in your
MST. Further, you have node H, for which you have two incident edges.
Out of those two edges, edge HI has the least cost, so you will include it
in MST structure.
How to Create MST Using Prim’s Algorithm
• Similarly, the inclusion of nodes G and E will happen in MST.

• After that, nodes E and C will get included. Now, from node C, you have
two incident edges. Edge CA has the tiniest edge weight. But its
inclusion will create a cycle in a tree structure, which you cannot allow.
How to Create MST Using Prim’s Algorithm
• And we will include edge CF in this minimum spanning tree structure.

The summation of all the edge weights in MST T(V’, E’) is equal to 30,
which is the least possible edge weight for any possible spanning tree
structure for this particular graph.
Example MST Using Prim’s Algorithm
Solution MST Using Prim’s Algorithm

You might also like