Git Product home page Git Product logo

prim-vs-kruskal's Introduction

Minimum Spanning Tree Algorithms

This Python script provides implementations of two well-known algorithms for finding the Minimum Spanning Tree (MST) of a connected, undirected graph: Prim's algorithm and Kruskal's algorithm.

Overview

A Minimum Spanning Tree is a subset of the edges of a connected, undirected graph that connects all the vertices together without any cycles and has the minimum possible total edge weight. This script offers efficient solutions to compute the MST using Prim's and Kruskal's algorithms.

Prim's Algorithm

Prim's algorithm starts with an arbitrary node and grows the MST by adding the shortest edge that connects a vertex in the MST to a vertex outside the MST. The process continues until all vertices are included in the MST.

Kruskal's Algorithm

Kruskal's algorithm follows a different approach. It starts with an empty graph and adds edges to the MST in ascending order of their weights, avoiding cycles in the process.

Usage

To use the script, import it into your project and call either the prim or kruskal function with the input graph. The graph should be represented as an adjacency matrix, where graph[i][j] represents the weight of the edge between vertices i and j. The output is a list of edges forming the MST.

# Example usage:
graph = [
    [0, 2, 0],
    [2, 0, 3],
    [0, 3, 0]
]
num_vertices = len(graph)

prim_out = prim(graph)
kruskal_out = kruskal(graph, num_vertices)

# Print the results
print("Prim's Algorithm MST: ")
for edge in prim_out:
    print(f"{edge[0]} - {edge[1]} (Weight: {edge[2]})")

print("\nKruskal's Algorithm MST: ")
for edge in kruskal_out:
    print(f"{edge[0]} - {edge[1]} (Weight: {edge[2]})")

prim-vs-kruskal's People

Contributors

mhmdsajadno avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.