0% found this document useful (0 votes)
35 views

Graph

A graph is a non-linear data structure consisting of vertices and edges. There are two main ways to represent a graph: an adjacency matrix uses a 2D matrix to represent edges between vertices, with 1s indicating an edge and 0s no edge; an adjacency list uses an array of linked lists where each index represents a vertex and stores its neighboring vertices. Prim's algorithm finds a minimum spanning tree of a weighted undirected graph by starting from an arbitrary vertex and adding the minimum weight edge that connects the partial spanning tree to a new vertex at each step.

Uploaded by

mugiii321
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Graph

A graph is a non-linear data structure consisting of vertices and edges. There are two main ways to represent a graph: an adjacency matrix uses a 2D matrix to represent edges between vertices, with 1s indicating an edge and 0s no edge; an adjacency list uses an array of linked lists where each index represents a vertex and stores its neighboring vertices. Prim's algorithm finds a minimum spanning tree of a weighted undirected graph by starting from an arbitrary vertex and adding the minimum weight edge that connects the partial spanning tree to a new vertex at each step.

Uploaded by

mugiii321
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Graph:

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. More formally a Graph is
composed of a set of vertices( V ) and a set of edges( E ). The graph is
denoted by G(E, V).

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 labeled/unlabelled
Representations of Graph
Here are the two most common ways to represent a graph :
Adjacency Matrix
Adjacency List
Adjacency Matrix
An adjacency matrix is a way of representing a graph as a matrix of
boolean (0’s and 1’s).
Let’s assume there are n vertices in the graph So, create a 2D
matrix adjMat[n][n] having dimension n x n.
If there is an edge from vertex i to j, mark adjMat[i][j] as 1.
If there is no edge from vertex i to j, mark adjMat[i][j] as 0.
Representation of Undirected Graph to Adjacency Matrix:
The below figure shows an undirected graph. Initially, the entire Matrix is
initialized to 0. If there is an edge from source to destination, we insert 1 to
both cases (adjMat[destination] and adjMat[destination]) because we
can go either way.

Undirected Graph to Adjacency Matrix

Representation of Directed Graph to Adjacency Matrix:


The below figure shows a directed graph. Initially, the entire Matrix is
initialized to 0. If there is an edge from source to destination, we
insert 1 for that particular adjMat[destination].

Directed Graph to Adjacency Matrix


Adjacency List
An array of Lists is used to store edges between two vertices. The size of
array is equal to the number of vertices (i.e, n). Each index in this array
represents a specific vertex in the graph. The entry at the index i of the
array contains a linked list containing the vertices that are adjacent to
vertex i.
Let’s assume there are n vertices in the graph So, create an array of list of
size n as adjList[n].
adjList[0] will have all the nodes which are connected (neighbour) to
vertex 0.
adjList[1] will have all the nodes which are connected (neighbour) to
vertex 1 and so on.
Representation of Undirected Graph to Adjacency list:
The below undirected graph has 3 vertices. So, an array of list will be
created of size 3, where each indices represent the vertices. Now, vertex 0
has two neighbours (i.e, 1 and 2). So, insert vertex 1 and 2 at indices 0 of
array. Similarly, For vertex 1, it has two neighbour (i.e, 2 and 1) So, insert
vertices 2 and 1 at indices 1 of array. Similarly, for vertex 2, insert its
neighbours in array of list.

Undirected Graph to Adjacency list

Representation of Directed Graph to Adjacency list:


The below directed graph has 3 vertices. So, an array of list will be created
of size 3, where each indices represent the vertices. Now, vertex 0 has no
neighbours. For vertex 1, it has two neighbour (i.e, 0 and 2) So, insert
vertices 0 and 2 at indices 1 of array. Similarly, for vertex 2, insert its
neighbours in array of list.
Types of Graphs

There are various types of graph algorithms that you would be


looking at in this article but before that, let's look at some types of
terms to imply the fundamental variations between them.

Order: Order defines the total number of vertices present in the


graph

Size: Size defines the number of edges present in the graph.

Self-loop: It is the edges that are connected from a vertex to itself.

Isolated vertex: It is the vertex that is not connected to any other


vertices in the graph.

Vertex degree: It is defined as the number of edges incident to a


vertex in a graph.

Weighted graph: A graph having value or weight of vertices.

Unweighted graph: A graph having no value or weight of vertices.

Directed graph: A graph having a direction indicator.

Undirected graph: A graph where no directions are defined.


Let's now carry forward the main discussion and learn about different
types of graph algorithms.

Breadth-First Search

Traversing or searching is one of the most used operations that are


undertaken while working on graphs. Therefore, in breadth-first-
search (BFS), you start at a particular vertex, and the algorithm tries to
visit all the neighbors at the given depth before moving on to the
next level of traversal of vertices. Unlike trees, graphs may contain
cyclic paths where the first and last vertices are remarkably the same
always. Thus, in BFS, you need to keep note of all the track of the
vertices you are visiting. To implement such an order, you use a
queue data structure which First-in, First-out approach. To understand
this, see the image given below.
Algorithm

 Start putting anyone vertices from the graph at the back of the
queue.
 First, move the front queue item and add it to the list of the
visited node.
 Next, create nodes of the adjacent vertex of that list and add
them which have not been visited yet.
 Keep repeating steps two and three until the queue is found to
be empty.

Pseudocode

Set all nodes to "not visited";


q = new Queue();
q.enqueue(initial node);
while ( q ? empty ) do
{
x = q.dequeue();
if ( x has not been visited )
{
visited[x] = true; // Visit node x !
for ( every edge (x, y) /* we are using all edges ! */ )
if ( y has not been visited )
q.enqueue(y); // Use the edge (x,y) !!!
}
}

Complexity: 0(V+E) where V is vertices and E is edges.

Applications

BFS algorithm has various applications. For example, it is used to


determine the shortest path and minimum spanning tree. It is also
used in web crawlers to creates web page indexes. It is also used as
powering search engines on social media networks and helps to find
out peer-to-peer networks in BitTorrent.

Depth-first search

In depth-first-search (DFS), you start by particularly from the vertex


and explore as much as you along all the branches before
backtracking. In DFS, it is essential to keep note of the tracks of visited
nodes, and for this, you use stack data structure.

Algorithm

 Start by putting one of the vertexes of the graph on the stack's


top.
 Put the top item of the stack and add it to the visited vertex list.
 Create a list of all the adjacent nodes of the vertex and then add
those nodes to the unvisited at the top of the stack.
 Keep repeating steps 2 and 3, and the stack becomes empty.

Pseudocode

DFS(G,v) ( v is the vertex where the search starts )


Stack S := {}; ( start with an empty stack )
for each vertex u, set visited[u] := false;
push S, v;
while (S is not empty) do
u := pop S;
if (not visited[u]) then
visited[u] := true;
for each unvisited neighbour w of uu
push S, w;
end if
end while
END DFS()

Recursion Tree Method:


The Recursion Tree Method is a way of solving recurrence relations. In this
method, a recurrence relation is converted into recursive trees. Each node
represents the cost incurred at various levels of recursion. To find the total
cost, costs of all levels are summed up.
Steps to solve recurrence relation using recursion tree method:
Draw a recursive tree for given recurrence relation
Calculate the cost at each level and count the total no of levels in the
recursion tree.
Count the total number of nodes in the last level and calculate the cost of
the last level
Sum up the cost of all the levels in the recursive tree
Let us see how to solve these recurrence relations with the help of some
examples:
Question 1: T(n) = 2T(n/2) + c
Solution:
Step 1: Draw a recursive tree
Recursion Tree

Step 2: Calculate the work done or cost at each level and count total no of
levels in recursion tree

Recursive Tree with each level cost

Count the total number of levels –


Choose the longest path from root node to leaf node
n/20 -→ n/21 -→ n/22 -→ ……… -→ n/2k
Size of problem at last level = n/2 k
At last level size of problem becomes 1
n/2k = 1
2k = n
k = log2(n)
Total no of levels in recursive tree = k +1 = log 2(n) + 1
Step 3: Count total number of nodes in the last level and calculate cost of
last level
No. of nodes at level 0 = 2 0 = 1
No. of nodes at level 1 = 2 1 = 2
………………………………………………………
No. of nodes at level log 2(n) = 2log2(n) = nlog2(2) = n
Cost of sub problems at level log 2(n) (last level) = nxT(1) = nx1 = n
Step 4: Sum up the cost all the levels in recursive tree
T(n) = c + 2c + 4c + —- + (no. of levels-1) times + last level cost
= c + 2c + 4c + —- + log 2(n) times + Θ(n)
= c(1 + 2 + 4 + —- + log 2(n) times) + Θ(n)
1 + 2 + 4 + —– + log 2(n) times –> 20 + 21 + 22 + —– + log2(n) times –>
Geometric Progression(G.P.)
= c(n) + Θ(n)
Thus, T(n) = Θ(n)
Prim’s algorithm:
starting from an arbitrary vertex, adding the minimum weight edge
that connects the tree to a new vertex, and repeating this process
until all vertices have been included in the tree

Algorithm:
Step 1: Determine an arbitrary vertex as the starting vertex of the MST.
Step 2: Follow steps 3 to 5 till there are vertices that are not included in
the MST (known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit

Example of prim's algorithm

Now, let's see the working of prim's algorithm using an example. It


will be easier to understand the prim's algorithm using an example.

Suppose, a weighted graph is -

Step 1 - First, we have to choose a vertex from the above graph. Let's
choose B.
Step 2 - Now, we have to choose and add the shortest edge from
vertex B. There are two edges from vertex B that are B to C with
weight 10 and edge B to D with weight 4. Among the edges, the edge
BD has the minimum weight. So, add it to the MST.

Step 3 - Now, again, choose the edge with the minimum weight
among all the other edges. In this case, the edges DE and CD are such
edges. Add them to MST and explore the adjacent of C, i.e., E and A.
So, select the edge DE and add it to the MST.

Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge
CE as it would create a cycle to the graph. So, choose the edge CA
and add it to the MST.

So, the graph produced in step 5 is the minimum spanning tree of the
given graph. The cost of the MST is given below -

Cost of MST = 4 + 2 + 1 + 3 = 10 units.

Kruskal's Algorithm

Kruskal's algorithm is a minimum spanning tree algorithm that takes a


graph as input and finds the subset of the edges of that graph which
form a tree that includes every vertex

has the minimum sum of weights among all the trees that can be
formed from the graph

Example of Kruskal's algorithm


Start with a weighted graph

Choose the edge with the least weight, if there are more than 1, choose
anyone

Choose the next shortest edge and add it


Choose the next shortest edge that doesn't create a cycle and add it

Choose the next shortest edge that doesn't create a cycle and add it

Repeat until you have a spanning tree


Kruskal Algorithm Pseudocode

Any minimum spanning tree algorithm revolves around checking if


adding an edge creates a loop or not.
The most common way to find this out is an algorithm called Union
FInd. The Union-Find algorithm divides the vertices into clusters and
allows us to check if two vertices belong to the same cluster or not
and hence decide whether adding an edge creates a cycle.
Kruskal_algorithm(Graph)
ANS <- ∅
for each Vertex v ∈ Graph.Vertex
do
Make_SET(v)
for each Edge (u, v) in Graph.Edge for increasing order
of weight(u, v)
do
if Find_in_SET(u) ≠ Find_in_SET(v)
do
ANS := ANS ∪ {(u, v)}
UNION(Find_in_SET(u), Find_in_SET(v))
return ANS

You might also like