Unit 5
Unit 5
Graphs
Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known as
vertices or nodes. Every node/vertex can be labelled or unlabelled.
Edges: Edges are drawn or used to connect two nodes of the graph. It can be an 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 labelled/unlabelled.
Directed graph: In a directed graph, the edges have a direction and can only be traversed in one
direction.
Undirected graph: In an undirected graph, the edges do not have a direction and can be traversed
in both directions.
Weighted graph: In a weighted graph, each edge has a weight or cost associated with it.
Unweighted graph: In an unweighted graph, each edge does not have any weight or cost
associated with it.
Connected graph: A connected graph is a graph in which every vertex is reachable from any other
vertex.
Disconnected graph: A disconnected graph is a graph in which some vertices are not connected
to any other vertices.
Cyclic graph: A cyclic graph is a graph that contains at least one cycle, i.e., a path that starts and
ends at the same vertex.
Acyclic graph: An acyclic graph is a graph that does not contain any cycles.
Adjacency List:
An adjacency list is a collection of linked lists, where each linked list represents the neighbours of a
vertex. For each vertex, we maintain a linked list of all the vertices adjacent to it. The adjacency list
representation is efficient for sparse graphs, where most of the vertex pairs do not have edges between
them. However, for dense graphs, it can be slower than the adjacency matrix representation.
ADJACENCY LIST
Adjacency Multi-lists:
In an adjacency multi-list, each vertex of the graph is represented by a node, and each node maintains a
list of all its adjacent vertices. However, in addition to the list of adjacent vertices, each node also
maintains a list of all the edges connecting the current node to its adjacent vertices. This extra
information makes adjacency multi-list more memory-intensive than the simple adjacency list
representation. However, it has the advantage of making it easy to traverse both the vertices and edges
of the graph in constant time.
NOTE: In summary, if the graph is dense, then the adjacency matrix is a good choice, and if the graph is
sparse, then the adjacency list is a good choice.
Depth-First Search
DFS (Depth-First Search) is a graph traversal algorithm that starts at a specified vertex in a graph
and explores as far as possible along each branch before backtracking. The algorithm works by
visiting a vertex, marking it as visited, and then recursively visiting all of its adjacent unvisited
vertices. This process continues until all reachable vertices have been visited.
This algorithm starts at a vertex and visits as far as possible along each branch before
backtracking. This means that the algorithm will first visit all the nodes in one path, and then
backtrack and visits the nodes in another path. When a dead-end occurs in any iteration, the
Depth First Search (DFS) method traverses a network in a deathward motion and uses a stack
data structure to remember to acquire the next vertex to start a search.
Breadth-First Search
BFS stands for Breadth-First Search, which is a graph traversal algorithm used to visit all the nodes
in a graph in a breadth-first manner. It starts at the root node and explores all the neighbour
nodes at the present depth before moving on to the nodes at the next depth level. The algorithm
works by maintaining a queue of nodes to be explored. It starts with the root node, adds it to the
queue, and marks it as visited. Then, it dequeues the node and adds all its unvisited neighbours
to the queue, marking them as visited. The algorithm repeats this process until all nodes have
been visited.
In BFS, the algorithm starts at a vertex and visits all of its neighbours before moving on to their
neighbours. This means that the algorithm will first visit all the nodes at the same level, and then
move on to the nodes at the next level. BFS is typically implemented using a queue, which stores
the vertices to be visited. Breadth-First Search uses a queue data structure to store the node and
mark it as "visited" until it marks all the neighbouring vertices directly related to it. The queue
operates on the First In First Out (FIFO) principle, so the node's neighbours will be viewed in the
order in which it inserts them in the node, starting with the node that was inserted first.
ORIGINAL GRAPH
Some of the possible spanning trees that can be created from the above graph are:
Spanning trees have many applications in computer science and other fields. For example, in network
design, a spanning tree can be used to ensure that all nodes in a network can communicate with each
other without creating any loops or redundancy.
Kruskal's algorithm works by sorting the edges of the graph by their weights and then iterating over the
edges in ascending order of weight. For each edge, the algorithm checks if adding it to the growing set
of edges in the MST creates a cycle. If not, the edge is added to the MST. If adding the edge creates a
cycle, it is discarded. The algorithm maintains a forest of trees, where each tree initially consists of a
single vertex. As edges are added to the MST, the trees are merged together until a single tree remains,
which is the MST.
Kruskal's algorithm has a time complexity of O(E log E), where E is the number of edges in the graph, due
to the sorting step. However, if the edges are already sorted, the algorithm has a time complexity of O(E
log V), where V is the number of vertices in the graph. Here is an example of Kruskal’s algorithm:
If you observe this graph, you’ll find two looping edges connecting the same node to itself again. And
you know that the tree structure can never include a loop or parallel edge. Hence, primarily you will need
to remove these edges from the graph structure.
The next step that you will proceed with is arranging all edges in a sorted list by their edge weights.
EDGES WEIGHT
EF 2
DF 2
CF 3
CB 3
CD 4
DE 5
BF 5
BD 6
AB 7
AC 8
After this step, you will include edges in the MST such that the included edge would not form a cycle in
your tree structure. The first edge that you will pick is edge EF, as it has a minimum edge weight that is
2.
All other edges should be discarded because these are created in a loop and the loop is prohibited in
Spanning Tree.
Choose any vertex from the graph and add it to the minimum spanning tree.
For all the vertices that are not in the minimum spanning tree, calculate the weight of the edge
that connects them to the vertices in the minimum spanning tree.
Select the vertex with the minimum weight edge and add it to the minimum spanning tree.
Repeat steps 2 and 3 until all the vertices in the graph are in the minimum spanning tree.
The algorithm can be implemented using a priority queue to efficiently select the vertex with the
minimum weight edge. The time complexity of Prim's algorithm is O(E log V), where E is the number of
edges and V is the number of vertices in the graph. This makes it an efficient algorithm for finding the
minimum spanning tree of a graph. Here is an example of Prim’s algorithm:
The algorithm then repeatedly selects the vertex with the smallest distance from the source vertex that
has not yet been visited and adds it to the visited set. For each neighbouring vertex that has not yet been
visited, the algorithm updates its distance from the source vertex if the new path through the current
vertex is shorter than the current shortest path. This process continues until all vertices have been
visited, and the shortest path to each vertex has been found. It is easier to start with an example and
then think about the algorithm.
Now, we are able to find the shortest path in this graph.
4.4.2 Floyd-Warshall Algorithm:
Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of vertices
in a weighted graph. This algorithm works for both the directed and undirected weighted graphs. But it
does not work for the graphs with negative cycles (where the sum of the edges in a cycle is negative).
Floyd-Warhshall algorithm is also called Floyd's algorithm, Roy-Floyd algorithm, Roy-Warshall algorithm,
or WFI algorithm. This algorithm follows the dynamic programming approach to find the shortest paths.
FILL EACH CELL WITH THE DISTANCE BETWEEN i th AND jth VERTEX
CALCULATE THE DISTANCE FROM THE SOURCE TO THE DESTINATION THROUGH THIS VERTEX K
Since A4 has minimum we ight from all t he e dges, thus it gives the shortest path between each
pair of vertices.