0% found this document useful (0 votes)
13 views15 pages

Ds 4

Data structure

Uploaded by

tchate45
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)
13 views15 pages

Ds 4

Data structure

Uploaded by

tchate45
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/ 15

CSPCC2002: Data Structures

UNIT 4 Graphs
• Definition,
• Basic Terminology,
• matrix representation implementation of graphs,
• graph Traversals,
• DFS,
• BFS,
• shortest Path,
• spanning tree

• Definition
Basic Terminology:
1. Graph:
• Graph is a non-linear data structure.
• Graph is a collection of vertices and edges but it contains cycle.

2. Degree:

• The total number of edges linked/connected to the vertex is called as degree


• In-degree: - No of total incoming edges to that particular vertex is known as In-degree.
• Out-degree: - No of total outgoing edges from that particular vertex is known as Out-degree.
• Source Vertex: - The vertex which has only outgoing edges and no incoming edges is known
as Source Vertex.
• Sink Vertex: - The vertex which has only incoming edges and no outgoing edges is known
as Sink vertex.
• Pendant Vertex: - The vertex which has only one incoming edge and no outgoing edges is
known as Pendant vertex.
• Isolated Vertex: - The vertex which has no incoming and no outgoing edges is known as
Isolated Vertex.

3. Successor: If there is a directed edge from vertex A to vertex B then vertex B is said to a
successor of vertex A.
4. Predecessor: If there is a directed edge from vertex A to vertex B then vertex A is said to a
Predecessor of vertex B.

5. Path: The sequence of successive edges from source vertex to destination vertex is known as
Path.

For the above graph paths can be A-B-C, A-B, and A-C, etc.

6. Articulation point:
• A vertex in an undirected connected graph is an articulation point if removing it disconnects
the graph.
• On removing the vertex the graph gets disconnected, then that vertex is called the
articulation point.
• Example:

7. Adjacent Vertex: Two vertices are called adjacent if there is an edge between them.
• Representation of Graph:
• Graph is a collection of vertices and edges but it contains cycle. It is a non-linear data structure.
• Graph data structure is represented using following representations:
1. Adjacency matrix
2. Adjacency list

1. Adjacency matrix:
• Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph.
• It is also called as bit matrix as it contains only two values i.e. 1 and 0.
• Value 1 indicates that there is an edge from vertex i to vertex j.
• Value 0 indicates that there is no edge from vertex i to vertex j.
• The adjacency matrix for the below example graph is:

Adjacency matrix for the above graph.


2. Adjacency List:
• In this representation, every vertex of graph contains list of its adjacent vertices.
• An array of linked lists is used.
• Size of the array is equal to number of vertices.

• Suppose we have array[]. An entry array[i] represents the linked list of vertices adjacent to the
ith vertex.

• This representation can also be used to represent a weighted graph.

• The weights of edges can be stored in nodes of linked lists.


• Adjacency list contains two columns as vertex name and adjacent vertices. It show who all are
adjacent vertices of each vertex in a graph.
• The adjacency list for the below example graph is:

Following is adjacency list representation of the above graph.


• graph Traversals

Graphs Traversal
To traverse a Graph means to start in one vertex, and go along the edges to visit other vertices
until all vertices, or as many as possible, have been visited.

Graph traversal is a process of visiting all the vertices or nodes in a graph, and it can be done in
two main ways:

Understanding how a Graph can be traversed is important for understanding how algorithms that
run on Graphs work.

The two most common ways a Graph can be traversed are:

• Depth First Search (DFS)


• Breadth First Search (BFS)

DFS is usually implemented using a Stack or by the use of recursion (which utilizes the call
stack), while BFS is usually implemented using a Queue.
- Depth First Search (DFS) Traversal

Depth First Search is said to go "deep" because it visits a vertex, then an adjacent vertex, and
then that vertex' adjacent vertex, and so on, and in this way the distance from the starting vertex
increases for each recursive iteration.

Depth-First Search (DFS):

• DFS explores as deeply as possible along a branch before backtracking.


• It uses a stack (or recursion) to keep track of the nodes to be visited.
• The algorithm proceeds as follows:
1. Visit the current node (e.g., print its value).
2. For each unvisited neighbor of the current node:
3. Recursively call DFS on the neighbor.

How it works:

1. Start DFS traversal on a vertex.


2. Do a recursive DFS traversal on each of the adjacent vertices as long as they are not
already visited. Run the animation below to see how Depth First Search (DFS) traversal
runs on a specific Graph, starting in vertex D (it is the same as the previous animation).

The DFS traversal starts in vertex D, marks vertex D as visited. Then, for every new
vertex visited, the traversal method is called recursively on all adjacent vertices that have
not been visited yet. So when vertex A is visited in the animation above, vertex C or
vertex E (depending on the implementation) is the next vertex where the traversal
continues.
- Breadth First Search (RFS) Traversal

Breadth First Search visits all adjacent vertices of a vertex before visiting neighboring vertices to
the adjacent vertices. This means that vertices with the same distance from the starting vertex are
visited before vertices further away from the starting vertex are visited.

How it works:

1. Put the starting vertex into the queue.


2. For each vertex taken from the queue, visit the vertex, then put all uninvited adjacent
vertices into the queue.
3. Continue as long as there are vertices in the queue.
4. Run the animation below to see how Breadth First Search (BFS) traversal runs on a
specific Graph, starting in vertex D.

As you can see in the animation above, BFS traversal visits vertices the same distance from the
starting vertex, before visiting vertices further away. So for example, after visiting vertex A,
vertex E and C are visited before visiting B, F and G because those vertices are further away.

Breadth First Search traversal works this way by putting all adjacent vertices in a queue (if they
are not already visited), and then using the queue to visit the next vertex.
- Shortest Path

The Shortest Path Problem

The shortest path problem is famous in the field of computer science.

To solve the shortest path problem means to find the shortest possible route or path between two
vertices (or nodes) in a Graph.

In the shortest path problem, a Graph can represent anything from a road network to a
communication network, where the vertices can be intersections, cities, or routers, and the edges
can be roads, flight paths, or data links.

The shortest path from vertex D to vertex F in the Graph above is D->E->C->F, with a total path
weight of 2+4+4=10. Other paths from D to F are also possible, but they have a higher total
weight, so they can not be considered to be the shortest path.

Solutions to The Shortest Path Problem

Dijkstra's algorithm and the Bellman-Ford algorithm find the shortest path from one start vertex,
to all other vertices.

To solve the shortest path problem means to check the edges inside the Graph until we find a
path where we can move from one vertex to another using the lowest possible combined weight
along the edges.

This sum of weights along the edges that make up a path is called a path cost or a path weight.
Algorithms that find the shortest paths, like Dijkstra's algorithm or the Bellman-Ford algorithm,
find the shortest paths from one start vertex to all other vertices.

To begin with, the algorithms set the distance from the start vertex to all vertices to be infinitely
long. And as the algorithms run, edges between the vertices are checked over and over, and
shorter paths might be found many times until the shortest paths are found at the end.

Every time an edge is checked and it leads to a shorter distance to a vertex being found and
updated, it is called a relaxation, or relaxing an edge.

Positive and Negative Edge Weights

Some algorithms that find the shortest paths, like Dijkstra's algorithm, can only find the shortest
paths in graphs where all the edges are positive. Such graphs with positive distances are also the
easiest to understand because we can think of the edges between vertices as distances between
locations.

If we interpret the edge weights as money lost by going from one vertex to another, a positive
edge weight of 4 from vertex A to C in the graph above means that we must spend $4 to go from
A to C.

But graphs can also have negative edges, and for such graphs the Bellman-Ford algorithm can be
used to find the shortest paths.
And similarly, if the edge weights represent money lost, the negative edge weight -3 from vertex
C to A in the graph above can be understood as an edge where there is more money to be made
than money lost by going from C to A. So if for example the cost of fuel is $5 going from C to
A, and we get paid $8 for picking up packages in C and delivering them in A, money lost is -3,
meaning we are actually earning $3 in total.

Negative Cycles in Shortest Path Problems

Finding the shortest paths becomes impossible if a graph has negative cycles.

Having a negative cycle means that there is a path where you can go in circles, and the edges that
make up this circle have a total path weight that is negative.

In the graph below, the path A->E->B->C->A is a negative cycle because the total path weight is
5+2-4-4=-1.

The reason why it is impossible to find the shortest paths in a graph with negative cycles is that it
will always be possible to continue running an algorithm to find even shorter paths.
Let's say for example that we are looking for the shortest distance from vertex D in graph above,
to all other vertices. At first we find the distance from D to E to be 3, by just walking the edge D-
>E. But after this, if we walk one round in the negative cycle E->B->C->A->E, then the distance
to E becomes 2. After walking one more round the distance becomes 1, which is even shorter,
and so on. We can always walk one more round in the negative cycle to find a shorter distance to
E, which means the shortest distance can never be found.

Luckily, the the Bellman-Ford algorithm, that runs on graphs with negative edges, can be
implemented with detection for negative cycles.

Understanding Shortest Path Algorithms:

• Shortest path algorithms aim to find the path between two nodes in a graph that
minimizes a specific metric (e.g., distance, cost, time).
• They are essential for various applications like network routing, transportation planning,
and game AI.
• Common shortest path algorithms include Dijkstra's algorithm, Bellman-Ford algorithm,
and Floyd-Warshall algorithm.

Dijkstra's Algorithm:

• Suitable for graphs with non-negative edge weights.


• Uses a priority queue to efficiently select the node with the shortest distance from the
source.
• The algorithm proceeds as follows:
1. Set the distance to the source node as 0 and the distance to all other nodes as
infinity.
2. Add the source node to the priority queue.
3. While the priority queue is not empty:
▪ Extract the node with the smallest distance from the queue.
▪ If its distance has changed, ignore it.
▪ For each neighbor of the extracted node:
▪ Calculate the tentative distance to the neighbor.
▪ If the tentative distance is less than the current distance to the
neighbor, update the neighbor's distance and add it to the priority
queue.

Bellman-Ford Algorithm:

• Handles graphs with negative edge weights (but detects negative cycles).
• Relaxes edges repeatedly until no further relaxation is possible.
• The algorithm proceeds as follows:
1. Initialize the distance to the source node as 0 and the distance to all other nodes as
infinity.
2. Relax all edges V-1 times (where V is the number of vertices).
3. If there's another relaxation possible after V-1 iterations, a negative cycle exists.
Floyd-Warshall Algorithm:

• Finds the shortest paths between all pairs of nodes in a graph.


• Uses dynamic programming to iteratively update distances.
• The algorithm proceeds as follows:
1. Initialize the distance matrix with the edge weights (or infinity if no edge exists).
2. For each intermediate vertex:
▪ For each pair of vertices:
▪ Check if using the intermediate vertex as a relay improves the
distance.

• Spanning tree
In this article, we are going to cover one of the most commonly asked DSA topic which is the
Spanning Tree with its definition, properties, and applications. Moreover, we will explore the
Minimum Spanning Tree and various algorithms used to construct it. These algorithms have
various pros and cons over each other depending on the use case of the problem.

What is a Spanning Tree?

A spanning tree is a subset of Graph G, such that all the vertices are connected using minimum
possible number of edges. Hence, a spanning tree does not have cycles and a graph may have
more than one spanning tree.
Properties of a Spanning Tree:
• A Spanning tree does not exist for a disconnected graph.
• For a connected graph having N vertices then the number of edges in the spanning tree for
that graph will be N-1.
• A Spanning tree does not have any cycle.
• We can construct a spanning tree for a complete graph by removing E-N+1 edges, where E is
the number of Edges and N is the number of vertices.
• Cayley’s Formula: It states that the number of spanning trees in a complete graph with N
vertices is
o For example: N=4, then maximum number of spanning tree possible =

= 16 (shown in the above image).

Real World Applications of A Spanning Tree:


• Several path finding algorithms, such as Dijkstra’s algorithm and A* search algorithm,
internally build a spanning tree as an intermediate step.
• Building Telecommunication Network.
• Image Segmentation to break an image into distinguishable components.
• Computer Network Routing Protocol
Minimum Spanning Tree(MST):
The weight of a spanning tree is determined by the sum of weight of all the edge involved in it.
Minimum Spanning Tree(MST):
The weight of a spanning tree is determined by the sum of weight of all the edge involved in it.
A minimum spanning tree (MST) is defined as a spanning tree that has the minimum
weight among all the possible spanning trees.
Properties of Minimum Spanning Tree:
• A minimum spanning tree connects all the vertices in the graph, ensuring that there is a path
between any pair of nodes.
• An MST is acyclic, meaning it contains no cycles. This property ensures that it remains a tree
and not a graph with loops.
• An MST with V vertices (where V is the number of vertices in the original graph) will have
exactly V – 1 edges, where V is the number of vertices.
• An MST is optimal for minimizing the total edge weight, but it may not necessarily be
unique.
• The cut property states that if you take any cut (a partition of the vertices into two sets) in the
original graph and consider the minimum-weight edge that crosses the cut, that edge is part of
the MST.

Minimum Spanning Tree of a Graph may not be Unique:


Like a spanning tree, there can also be many possible MSTs for a graph as shown in the below
image:

Algorithms to Find Minimum Spanning Tree of a Graph:


There are several algorithms to find the minimum spanning tree from a given graph, some of
them are listed below:
1. Krushkal’s MST Algorithm
2. Prim’s MST Algorithm
3. Boruvka’s Algorithm
4. Reverse-Delete Algorithm

You might also like