Ds 4
Ds 4
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:
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:
• Suppose we have array[]. An entry array[i] represents the linked list of vertices adjacent to the
ith vertex.
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.
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.
How it works:
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:
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
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.
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.
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.
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.
• 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:
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:
• 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.
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 =