0% found this document useful (0 votes)
4 views13 pages

Unit 5 Aads

The Bellman-Ford algorithm finds the shortest paths from a single source vertex to all other vertices in a weighted graph, accommodating negative-weight edges and detecting negative cycles, with a time complexity of O(n^2). Kruskal's Algorithm, on the other hand, is used to find the Minimum Spanning Tree (MST) of a graph by sorting edges and using a Union-Find data structure to manage and merge sets of vertices, with a time complexity of O(E log E) for sorting and O(E α(V)) for union-find operations. Both algorithms have specific strengths and weaknesses, particularly in handling negative weights and cycle detection.

Uploaded by

carey26117
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)
4 views13 pages

Unit 5 Aads

The Bellman-Ford algorithm finds the shortest paths from a single source vertex to all other vertices in a weighted graph, accommodating negative-weight edges and detecting negative cycles, with a time complexity of O(n^2). Kruskal's Algorithm, on the other hand, is used to find the Minimum Spanning Tree (MST) of a graph by sorting edges and using a Union-Find data structure to manage and merge sets of vertices, with a time complexity of O(E log E) for sorting and O(E α(V)) for union-find operations. Both algorithms have specific strengths and weaknesses, particularly in handling negative weights and cycle detection.

Uploaded by

carey26117
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/ 13

Bellman-Ford algorithm

► The Bellman-Ford algorithm operates on an input graph, G,


with Iv I v�rti_c��nYJ I E | �tfges

► The Bellman-Ford algorithm is a graph algorithm used to find the shortest paths
from a single source vertex to all other vertices in a weighted graph. It can handle
graphs with negative-weight edges, unlike Dijkstra's algorithm.

► Bellman-Ford algorith[ll is a �ingl_e-source shortest path algorithm


► Bellman-Ford can also detect negative edge cycles which is a useful feature and
dijkstra's can't detect negative
► The main contribution of this algorithm is in its ordering of relaxations
► Time complexity: O(n2)
► drawback: it fails where there is a negative weight cycle present
Kruskal’s Algorithm
Purpose: Finds the Minimum Spanning Tree (MST) of a graph.

Steps:
Sort all edges by their weights.
Initialize a forest where each vertex is its own tree.

Process edges in ascending order:


If an edge connects two different trees, include it in the MST and merge the
two trees.
If it connects two vertices in the same tree, discard it (to avoid cycles).

Time Complexity:
Sorting edges: O(ElogE).
Union-Find operations: O(E α(V)), where α is the inverse Ackermann
function (very slow-growing).
The Union-Find (or Disjoint Set Union) data structure is used to efficiently manage and
merge sets of vertices while applying Kruskal’s Algorithm to find a Minimum Spanning
Tree (MST). It helps to check whether adding an edge creates a cycle and ensures the graph
remains acyclic.

Key Operations:
Find: Determines which set a particular element belongs to. This helps check if two
vertices are already connected.
Union: Merges two sets. This is used to connect two vertices by adding an edge to the
MST.

Steps in Kruskal’s Algorithm:


Sort all edges by weight.
Use the Union-Find structure:
For each edge, perform Find on both endpoints.
If they belong to different sets, add the edge to the MST and perform Union to merge the
sets.
If they belong to the same set, skip the edge to avoid cycles.

Optimizations:
Path Compression: Speeds up the Find operation by directly linking elements to their root.
Union by Rank/Size: Ensures smaller trees are attached under larger ones to keep the
structure shallow.

You might also like