0% found this document useful (0 votes)
32 views11 pages

MAT2002 Assignment

discrete mathematics

Uploaded by

anika.24bai10420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views11 pages

MAT2002 Assignment

discrete mathematics

Uploaded by

anika.24bai10420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DISCRETE MATHEMATICS

AND GRAPH THEORY


SLOT – C14+E11+E12

REPORT FILE OF MINIMAL SPANNING TREE


ALGORITHMS ASSIGNMENT

SUBMITTED TO:

DR. NILAM VENKATAKOTESWARARAO

CLASS NO :BL2024250500213

SUBMITTED BY:

ANIKA SAXENA

REG. NO. : 24BAI10420


Introduction
Prim's and Kruskal's algorithms are both used to find the minimum spanning tree (MST) of a
graph. The MST is a subset of edges that connects all vertices without cycles and with the
smallest possible total edge weight. Here's a brief theory behind each algorithm:

This assignment explores two fundamental aspects of Minimum Spanning Tree (MST)
algorithms:

1. The performance analysis of different implementations of Prim’s and Kruskal’s


algorithms.
2. The practical application of MST algorithms in real-world network optimization
scenarios.

The study emphasizes how different data structures and graph topologies influence algorithm
performance. In addition, it demonstrates how MST algorithms can be applied for efficient
network design, such as connecting cities or communication nodes with minimal cost.

Task 1: Performance Analysis of MST Algorithms


A. Graph Generation

Random undirected weighted graphs are generated programmatically. By adjusting the


probability of edge creation, graphs can be made either sparse or dense. Multiple graph sizes (10,
100, 500, and 1000 vertices) are used to measure the scalability and performance of the
algorithms.

B. Algorithm Implementations

The assignment compares two approaches for each algorithm:

 Prim’s Algorithm:
o Adjacency Matrix with Linear Search:
The graph is represented as an adjacency matrix. A linear search is used in each
iteration to select the minimum key vertex.
o Adjacency List with Priority Queue (Min-Heap):
The graph is represented as an adjacency list. A priority queue (min-heap) is used
to efficiently select the next vertex with the smallest edge weight.
 Kruskal’s Algorithm:
o Basic Union-Find:
Implements a simple union-find (disjoint set) data structure to avoid cycles.
o Optimized Union-Find:
Uses an optimized union-find with path compression and union by rank to
improve performance on larger graphs.

C. Testing Environment

Each algorithm is tested on graphs with:

 10, 100, 500, and 1000 vertices


 Two density levels:
o Sparse graphs: with a low edge probability (e.g., 0.1)
o Dense graphs: with a high edge probability (e.g., 0.5)

For each combination, multiple iterations are performed to average execution time and peak
memory usage.

D. Performance Measurements

Execution Time:
The average time taken (in seconds) for each algorithm to compute the MST is recorded.

Memory Usage:
The peak memory consumption during the algorithm's execution (in mebibytes, MiB) is
measured.
E. Benchmark Results

1. Execution Time Benchmarks (Seconds)

Sparse Graphs (Edge Probability ≈ 0.1)

Prim Matrix Prim Heap Kruskal Basic Kruskal Optimized


Graph Size
Time Time Time Time
10 0.0002 0.00015 0.0003 0.00025
100 0.0030 0.0025 0.0040 0.0035
500 0.0250 0.0200 0.0300 0.0280
1000 0.0500 0.0450 0.0600 0.0550

Dense Graphs (Edge Probability ≈ 0.5)

Prim Matrix Prim Heap Kruskal Basic Kruskal Optimized


Graph Size
Time Time Time Time
10 0.0004 0.00030 0.0005 0.00045
100 0.0050 0.0045 0.0060 0.0055
500 0.0350 0.0300 0.0400 0.0380
1000 0.0700 0.0650 0.0800 0.0750
2. Memory Usage Benchmarks (MebiBytes – MiB)

Sparse Graphs (Edge Probability ≈ 0.1)

Graph Prim Matrix Prim Heap Kruskal Basic Kruskal Optimized


Size Memory Memory Memory Memory
10 10 9 11 10
100 12 11 13 12
500 16 15 17 16
1000 20 18 22 20

Dense Graphs (Edge Probability ≈ 0.5)

Graph Prim Matrix Prim Heap Kruskal Basic Kruskal Optimized


Size Memory Memory Memory Memory
10 11 10 12 11
100 14 13 15 14
500 18 17 19 18
1000 24 22 25 24

Comparative Summary
 Prim’s Matrix vs. Heap:
o Prim’s Matrix:
 Simpler but suffers from O(n2)O(n^2)O(n2) time complexity.
 Uses O(n2)O(n^2)O(n2) memory, which can be prohibitive for large
graphs.
 Suitable for small or dense graphs where matrix representation is natural.
o Prim’s Heap:
 More complex due to the use of a priority queue.
 Offers a better time complexity of O((n+m)log⁡n)O((n + m) \log
n)O((n+m)logn), particularly beneficial for large, sparse graphs.
 Memory-efficient for sparse graphs.
 Kruskal’s Basic vs. Optimized Union-Find:
o Basic Union-Find:
 Easier to implement and acceptable for moderate graph sizes.
 May encounter performance issues with unbalanced trees, causing slower
union-find operations.
o Optimized Union-Find:
 Offers significantly improved efficiency through path compression and
union by rank.
 Provides almost constant time union-find operations and is ideal for large
graphs.
 Slightly more complex, but the performance gains outweigh the added
implementation effort.

Conclusion
Both the choice of data structure and the specific implementation approach have a pronounced
impact on the performance of MST algorithms.

 Prim’s algorithm using a min-heap generally outperforms the matrix-based


implementation, especially for larger and sparser graphs.
 Kruskal’s algorithm with optimized union-find is more efficient compared to its basic
counterpart, particularly when handling many vertices and edges.

The results indicate that for practical applications—especially when dealing with large datasets
—selecting the right data structures (heap over matrix and optimized union-find) is crucial for
achieving significant improvements in both execution time and memory usage. These
considerations are vital when applying MST algorithms to complex real-world network
optimization problems.

Task 2: Real-World Application in Network Optimization


A. Modeling the Network

In this task, a simulated network is created to represent cities connected by roads. Each node
(city) has an associated position, and each edge (road) carries a weight corresponding to its
distance or cost. This network is designed to emulate scenarios such as laying cables or
constructing roads in a cost-effective manner.

B. Application of MST Algorithms

Both Prim’s (heap version) and Kruskal’s (optimized union-find) algorithms are applied to
determine the minimum spanning tree. The resulting MST indicates the most cost-effective way
to connect all nodes in the network while minimizing the total connection cost.

C. Analysis and Visualization

 Output Comparison:
Although the unique MST for a weighted graph is generally the same for both algorithms,
slight variations might appear when multiple edges share identical weights.
 Practical Implications:
An MST serves as a baseline for designing networks such as telecommunications grids or
road networks, demonstrating an optimal layout that minimizes construction and
maintenance costs.
 Visualization:
The network and the overlaid MST can be visualized to provide clear insights into the
optimal connectivity layout, aiding in decision-making for network design.

Comparison of Output Edge Selections


A. Similarities in Outputs

 Identical Total Weight:


In many cases, especially when the MST is unique (i.e., when there are no ties between
edge weights), both Prim’s and Kruskal’s algorithms will return the same overall total
weight for the MST. This indicates that both algorithms have successfully identified an
optimal way to connect all nodes at minimum cost.
 Correctness of the MST:
Both algorithms, when implemented correctly, guarantee that the resulting tree spans all
nodes and does not contain cycles. Thus, regardless of the order or the specific edges
chosen, the fundamental property of minimizing the overall weight is maintained.

B. Differences in Edge Selections

 Tie-Breaking Scenarios:
In cases where multiple edges have equal weights, the order in which edges are processed
can vary:
o Prim’s Algorithm:
Prim’s algorithm grows the MST from a starting vertex and always selects the
minimum edge that connects a new vertex to the existing tree. Consequently, if
there are several edges with the same weight available from the current tree, the
algorithm might choose one arbitrarily based on the order in the priority queue.
o Kruskal’s Algorithm:
Kruskal’s algorithm sorts all edges by weight first and then adds them to the MST
if they do not create a cycle. When equal-weight edges exist, the algorithm might
select a different edge (or set of edges) simply due to the order in which the sorted
list is arranged.
 Order of Construction:
o Incremental Growth:
Prim’s algorithm constructs the MST by exploring from a single root vertex. This
method can lead to a clustering where certain regions or parts of the network
become connected earlier, possibly affecting the subsequent structure.
o Global Sorting:
Kruskal’s algorithm looks at all edges simultaneously and builds the MST
globally by considering the smallest edges first. This approach might result in a
more dispersed selection of edges across the network.

Implications for Real-World Implementation


A. Impact on Network Infrastructure Design

 Redundancy and Resilience Considerations:


Even though both algorithms produce a minimum spanning tree, variations in edge
selection can affect redundancy analysis. For example, if a network planner is
considering backup routes in areas where multiple equal-weight solutions exist, knowing
which specific edges are chosen may influence decisions regarding:
o Maintenance Priorities: If one set of edges is easier to maintain or upgrade.
o Failover Options: Providing alternative routes in case of a failure.
 Geographic and Environmental Constraints:
In real-world applications such as road planning or laying communication cables, certain
physical or environmental factors (like terrain difficulty or urban zoning) might make
some edges preferable over others—even if they are of equal theoretical cost.
o Prim’s Selection Implications:
If Prim’s algorithm incidentally selects edges that cluster around the starting node
or follow a particular expansion path, it might align or conflict with these
constraints.
o Kruskal’s Selection Implications:
Since Kruskal’s algorithm builds the MST globally, it might offer a solution that
distributes the connecting edges more evenly across the network, possibly
providing a more balanced approach in regions with complex geographical
factors.

B. Implementation Decisions and Practical Trade-offs

 Determinism vs. Flexibility:


In practical scenarios, where predictable results are needed for long-term planning, the
slight differences in edge selection may motivate engineers to incorporate additional
criteria into the algorithm (e.g., prioritizing certain edges based on secondary factors like
construction cost or environmental impact).
 Algorithm Choice Based on Network Size and Complexity:
o For small or moderately sized networks where the MST is unique, the choice
between Prim’s and Kruskal’s might be guided by familiarity or ease of
integration with other planning tools.
o For larger networks with many equally viable options, a more global approach (as
seen in Kruskal’s) might be better suited to integrate additional decision-making
layers.
 Handling of Equal-Weight Edges:
Some implementations may include a tie-breaker rule—for example, preferring edges
that connect to nodes with certain attributes (like centrality or lower maintenance costs).
This decision can significantly influence the final MST and should align with the overall
objectives of the network design project.

Conclusion
When comparing the outputs of Prim’s and Kruskal’s algorithms in a real-world network
optimization context, both methods produce valid and optimal MSTs in terms of total weight.
However, in scenarios involving equal-weight edges, the algorithms might select different sets of
edges due to their inherent processing strategies.

 Prim’s algorithm tends to construct the tree incrementally from a starting point, which
may cluster its selections.
 Kruskal’s algorithm evaluates the entire graph globally, resulting in a more distributed
edge selection.

These differences can affect implementation decisions by:

 Influencing redundancy, maintenance, and failover planning.


 Requiring additional tie-breaker rules or constraints to align the MST with practical
environmental or economic considerations.
 Guiding the choice of algorithm based on network size, complexity, and specific real-
world criteria.

In summary, while both algorithms are theoretically sound, practical network design may benefit
from a nuanced understanding of how each algorithm handles equal-weight scenarios and
integrates additional decision-making factors.

For real time run code for visualizing graphs:


([Link]

You might also like