0% found this document useful (0 votes)
7 views

Discrete Structures Lecture 12..

Uploaded by

rg709pk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Discrete Structures Lecture 12..

Uploaded by

rg709pk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Greedy Algorithm (Kruskal's

Algorithm for Minimum


Spanning Tree)
INTRODUCTION
3
EXPLANATION AND
EXAMPLE
4
CONTENT PSEUDO CODE WITH
EXAMPLE
5
ADVANTAGES AND
DISADVANTAGES
6
USAGE IN REAL LIFE
11
MINIMUM SPANNING TREE
(MST)SPANNING TREE:
MINIMUM
An MST of a graph is a subgraph that
includes all the vertices (nodes) and is
connected without cycles, using the
least total edge weight.
PROPERTIES:
A graph may have many spanning trees,
but the MST has the smallest possible
total edge weight.
It is useful in applications where we
want to connect all points in the least
costly way, such as in network design
(e.g., roads, electrical grids).
.
GREEDY APPROACH

The Greedy algorithm always picks the best option


available at each step, hoping this leads to the overall
best solution.
In MST:
At each step, the algorithm picks the edge with the
smallest weight that doesn’t form a cycle, ensuring it
builds a low-cost, connected graph
KRUSKAL’S ALGORITHM FOR MST
Sort Edges:
• First, sort all the edges of the graph in increasing order based on
their weights.
• This sorting ensures we always consider the lightest (smallest
weight) edges first, which is a core feature of the greedy
strategy.
Pick Edges:
• Starting with the smallest edge, add it to the MST if it doesn’t
form a cycle.
• The key here is that we only want to include edges that connect
different components (i.e., they don’t form cycles).
Cycle Detection (Union-Find):
Use a Union-Find (also called Disjoint Set) data structure to
manage which vertices are connected.
Union-Find helps check if adding an edge will form a cycle by
checking if the two vertices of the edge are already in the same
component.

Repeat:

they don’t form cycles, until the MST has 𝑛−1 edges (for a graph
Continue picking the smallest edges, adding them to the MST if

with 𝑛 nodes).
EXAMPLE OF KRUSKUL ALGORITHIM
KRUSKAL’S ALGORITHM PESUDO
The Correctness of Kruskal’s Algorithm
Assume the Algorithm is Wrong:
Suppose Kruskal's algorithm gives a wrong result, meaning it doesn't
produce an MST.
Adding a Wrong Edge:
If the algorithm adds a wrong edge, there must be a lighter edge
that should have been chosen instead.
Cut-and-Paste Argument:
If we split the graph into two parts, the smallest edge between them
must be in the MST. The algorithm always picks the smallest edge, so it
can’t pick a wrong one.
Contradiction:
Since the algorithm always picks the smallest edge, it cannot make a
wrong choice, proving it must produce the correct MST.
TIME COMPLEXITY:
Sorting the Edges:
1.Kruskal's algorithm starts by sorting all the edges based on
their weight.
2.Sorting takes O(E log E) time, where E is the number of edges.
3.Example: If there are 10 edges, sorting them based on weight
takes time proportional to 10 log 10.
Union-Find for Cycle Detection:
4.To avoid cycles and merge components, the Union-Find data
structure is used.
5.This step takes O(E log V) time, where V is the number of
vertices and E is the number of edges.
6.Example: For 10 edges and 5 vertices, checking and merging
takes time proportional to 10 log 5.
ADVANTAGES OF KRUSKAL'S
ALGORITHM
Efficient and Quick:
1.Works well for small and medium-sized networks.
2.Example: Designing road maps or cable connections.
Cost-Effective:
3.Ensures the lowest total cost for connecting points like
electricity cables or internet wires.
Straightforward:
4. Simple steps make it easy to understand and use in practical
scenarios.
5. Can efficiently combine separate Handles Disconnected Graphs:
6. networks.
7.Example: Connecting rural power grids.
DISADVANTAGES

1.NOT ALWAYS THE BEST CHOICE:


Doesn’t account for future changes or priorities.
2.SORTING IS REQUIRED:
Sorting edges can be time-consuming for very large networks.
3.NOT DYNAMIC:
Adding new nodes or edges requires rerunning the algorithm.

You might also like