Skip to main content

Библиотека для работы с графами и алгоритмами, включая алгоритмы Крускала, Гамильтона и минимального остовного дерева

Project description

Minimal Spanning Tree and Hamiltonian Cycle

This project implements algorithms for finding the minimum spanning tree (MST) and the Hamiltonian cycle in graphs. It includes three main classes: Kruskal', HamiltonandPrimal_min', each of which provides different methods for working with graphs.

Installation

To work with the project, you need to install the following libraries:

pip install networkx matplotlib

Description of classes

1. Kruskal

The `Kruskal' class implements the Kruskal algorithm for finding the minimum spanning tree.

Constructor

Kruskal(points, edges)
  • points: A list of coordinates of graph vertices.
  • edges: A list of graph edges, where each edge is represented as a tuple (u, v, weight).

Methods

  • draw_only(k): Displays the graph and its edges.
  • `view(min_edges, max_edges, k)': Displays a graph with a minimum and maximum spanning tree.
  • kruskals_algorithm(edges): Implements the Kraskal algorithm for finding MST.
  • `sort_edges_min()': Sorts the edges in ascending order of weight.
  • `sort_edges_max()': Sorts the edges in descending order of weight.
  • result_weight(edges): Calculates and outputs the total weight of the tree.

2. Hamilton

The `Hamilton' class implements an algorithm for finding a Hamiltonian cycle.

Constructor

Hamilton(points, edges)
  • points: A list of coordinates of graph vertices.
  • edges: A list of graph edges in the form of pairs of vertex indexes.

Methods

  • cycle_exist(cycle): Checks for a Hamiltonian cycle and outputs it.
  • `draw()': Visualizes the graph.
  • `avoidable()': Outputs the available vertices.
  • `hamiltonian_cycle(start)': Finds a Hamiltonian cycle starting from a given vertex.
  • draw_graph(path=None): Displays a graph with a highlighted Hamiltonian cycle.

3. Primal_min

The `Primal_min' class implements the Prim algorithm for finding the minimum spanning tree.

Constructor

Primal_min(graph)
  • `graph': A graph in the form of a dictionary, where the keys are vertices and the values are lists of edges with weights.

Methods

  • run(begin): Starts the Prim algorithm, starting from the specified vertex.
  • `min_tree()': Outputs the minimum spanning tree and its total weight.

Usage example

# Example of creating a graph and finding a minimum spanning tree
points = [(0, 0), (1, 1), (2, 0), (1, -1)]
edges = [(0, 1, 1.5), (0, 2, 1.0), (1, 3, 2.0), (2, 3, 1.0)]

kruskal = Kruskal(points, edges)
sorted_edges = kruskal.sort_edges_min()
mst = kruskal.alg_Kraskala(sorted_edges)
kruskal.view(mst, sorted_edges, 3)

# An example of finding a Hamiltonian cycle
hamilton = Hamilton(points, edges)
cycle = hamilton.hamiltonian_cycle(0)
hamilton.cycle_exist(cycle)
hamilton.draw_graph(cycle)

License

This project is licensed under the MIT License. Please review the LICENSE file for more information. ``

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kruskalhamiltonprimal-0.0.2.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

KruskalHamiltonPrimal-0.0.2-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file kruskalhamiltonprimal-0.0.2.tar.gz.

File metadata

  • Download URL: kruskalhamiltonprimal-0.0.2.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for kruskalhamiltonprimal-0.0.2.tar.gz
Algorithm Hash digest
SHA256 142ef1bfb8d5b07cc95405022624b2baf3e7147d0d6dd90ad9c188174c4548f4
MD5 074443b7f3f0e4ed6d781bdc8813821b
BLAKE2b-256 f9324317ac58b1e4bfe1efbb439e9014226e6a8ff7dfdecef87665db88060954

See more details on using hashes here.

File details

Details for the file KruskalHamiltonPrimal-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for KruskalHamiltonPrimal-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fc3c1621fd25e505c2e7d4c3520a0866f95aad1dcc60ac840fed8e97a0bceb6
MD5 60879a87bcc487949db55c81a4bfa4ef
BLAKE2b-256 c2881ea53c6518ba6da9c7a65918be7c5e119ee8e7189e0a078768ed5641c74f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page