You are on page 1of 13

Graph

A graph is an abstract data type (ADT) that consists of a set of objects


that are connected to each other via links. These objects are
called vertices and the links are called edges.
Usually, a graph is represented as G = {V, E}, where G is the graph
space, V is the set of vertices and E is the set of edges. If E is empty, the
graph is known as a forest.

Representation of Graphs

While representing graphs, we must carefully depict the elements


(vertices and edges) present in the graph and the relationship between
them. Pictorially, a graph is represented with a finite set of nodes and
connecting links between them. However, we can also represent the
graph in other most commonly used ways, like −

 Adjacency Matrix
 Adjacency List
Adjacency Matrix

The Adjacency Matrix is a V×V matrix where the values are filled with
either 0 or 1. If the link exists between Vi and Vj, it is recorded 1;
otherwise, 0.

For the given graph below, let us construct an adjacency matrix −


The adjacency matrix is −

Adjacency List

The adjacency list is a list of the vertices directly connected to the other
vertices in the graph.
The adjacency list is −

Types of graph

There are two basic types of graph −

 Directed Graph
 Undirected Graph

Directed graph, as the name suggests, consists of edges that possess a


direction that goes either away from a vertex or towards the vertex.
Undirected graphs have edges that are not directed at all.
2. undirected graph

An undirected graph is a type of graph where the edges have no


specified direction assigned to the them.

Example of undirected graph

Characteristics of an Undirected Graph:

 Edges in an undirected graph are bidirectional in nature.


 In an undirected graph, there is no concept of a “parent” or “child”
vertex as there is no direction to the edges.
 An undirected graph may contain loops, which are edges that
connect a vertex to itself.
 Degree of each vertex is the same as the total no of edges connected
to it.

Applications of Undirected Graph:

 Social Networks: Undirected graphs are used to model social


networks where people are represented by nodes and the connections
between them are represented by edges.
 Traffic flow optimization: Undirected graphs are used in traffic
flow optimization to model the flow of vehicles on road networks.
The vertices of the graph represent intersections or road segments,
and the edges represent the connections between them. The graph
can be used to optimize traffic flow and plan transportation
infrastructure.
 Website analysis: Undirected graphs can be used to analyze the
links between web pages on the internet. Each web page is
represented by a vertex, and each link between web pages is
represented by an edge.

Shortest Path

The shortest path in a graph is defined as the minimum cost route from
one vertex to another. This is most commonly seen in weighted directed
graphs but are also applicable to undirected graphs.

A popular real-world application of finding the shortest path in a graph


is a map. Navigation is made easier and simpler with the various shortest
path algorithms where destinations are considered vertices of the graph
and routes are the edges. The two common shortest path algorithms are

 Dijkstra’s Shortest Path Algorithm


 Bellman Ford’s Shortest Path Algorithm

Graph Traversal Technique:


1 DFS (Depth First Search)

Depth First Search (DFS) algorithm traverses a graph in a depthward


motion and uses a stack to remember to get the next vertex to start a
search, when a dead end occurs in any iteration.

As in the example given above, DFS algorithm traverses from S to A to


D to G to E to B first, then to F and lastly to C. It employs the following
rules.

 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited.


Display it. Push it in a stack.
 Rule 2 − If no adjacent vertex is found, pop up a vertex from the
stack. (It will pop up all the vertices from the stack, which do not
have adjacent vertices.)
 Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
Step Traversal Description

1 Initialize the stack.

Mark S as visited and


put it onto the stack.
Explore any unvisited
adjacent node from S.
We have three nodes
2
and we can pick any
of them. For this
example, we shall
take the node in an
alphabetical order.
Mark A as visited and
put it onto the stack.
Explore any unvisited
adjacent node from A.
3
Both S and D are
adjacent to A but we
are concerned for
unvisited nodes only.
Visit D and mark it as
visited and put onto
the stack. Here, we
have B and C nodes,
which are adjacent
4
to D and both are
unvisited. However,
we shall again choose
in an alphabetical
order.

We choose B, mark it
as visited and put
onto the stack.
5 Here B does not have
any unvisited adjacent
node. So, we
pop B from the stack.

We check the stack


top for return to the
previous node and
6 check if it has any
unvisited nodes. Here,
we find D to be on the
top of the stack.
Only unvisited
adjacent node is
from D is C now. So
7
we visit C, mark it as
visited and put it onto
the stack.

As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node that has an unvisited adjacent node. In this
case, there's none and we keep popping until the stack is empty.

2. Breadth First Search (BFS)

Breadth First Search (BFS) algorithm traverses a graph in a breadthward


motion and uses a queue to remember to get the next vertex to start a
search, when a dead end occurs in any iteration.
As in the example given above, BFS algorithm traverses from A to B to
E to F first then to C and G lastly to D. It employs the following rules.

 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited.


Display it. Insert it in a queue.
 Rule 2 − If no adjacent vertex is found, remove the first vertex
from the queue.
 Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
Step Traversal Description

1 Initialize the queue.

We start from
visiting S (starting
2
node), and mark it as
visited.

We then see an
unvisited adjacent
node from S. In this
example, we have
3 three nodes but
alphabetically we
choose A, mark it as
visited and enqueue
it.
Next, the unvisited
adjacent node
4 from S is B. We mark
it as visited and
enqueue it.

Next, the unvisited


adjacent node
5 from S is C. We mark
it as visited and
enqueue it.

Now, S is left with no


unvisited adjacent
6
nodes. So, we
dequeue and find A.

From A we have D as
unvisited adjacent
7 node. We mark it as
visited and enqueue
it.
At this stage, we are left with no unmarked (unvisited) nodes. But as per
the algorithm we keep on dequeuing in order to get all unvisited nodes.
When the queue gets emptied, the program is over.

Spanning Tree
A spanning tree is a subset of an undirected graph that contains all the
vertices of the graph connected with the minimum number of edges in
the graph. Precisely, the edges of the spanning tree is a subset of the
edges in the original graph.

If all the vertices are connected in a graph, then there exists at least one
spanning tree. In a graph, there may exist more than one spanning tree.

Properties
 A spanning tree does not have any cycle.
 Any vertex can be reached from any other vertex.

Example

In the following graph, the highlighted edges form a spanning tree.


Minimum Spanning Tree (MST)

 In a weighted graph, a minimum spanning tree is a spanning tree


that has minimum weight than all other spanning trees of the same
graph. In real-world situations, this weight can be measured as
distance, congestion, traffic load or any arbitrary value denoted to
the edges.

 Minimum Spanning-Tree Algorithm

 Kruskal's Algorithm
 Prim's Algorithm

You might also like