Skip to content

Commit

Permalink
Merge 122404f into 3f9aa45
Browse files Browse the repository at this point in the history
  • Loading branch information
dunderbruno committed Sep 28, 2018
2 parents 3f9aa45 + 122404f commit 59eba87
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ If you want to uninstall algorithms, it is as simple as:
- [satisfiability](algorithms/graph/satisfiability.py)
- [tarjan](algorithms/graph/tarjan.py)
- [traversal](algorithms/graph/traversal.py)
- [prim](algorithms/graph/prim.py)
- [heap](algorithms/heap)
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
- [skyline](algorithms/heap/skyline.py)
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pip3 uninstall -y algorithms
- [graph:图](algorithms/graph/graph.py)
- [traversal:遍历](algorithms/graph/traversal.py)
- [markov_chain:马尔可夫链](algorithms/graph/markov_chain.py)
- [prim](algorithms/graph/prim.py)
- [heap:堆](algorithms/heap)
- [merge_sorted_k_lists:合并k个有序链](algorithms/heap/merge_sorted_k_lists.py)
- [skyline:天际线](algorithms/heap/skyline.py)
Expand Down
1 change: 1 addition & 0 deletions README_FR.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Si vous voulez désinstaller le paquet algorithms, il suffit de procéder comme
- [satisfiability](algorithms/graph/satisfiability.py)
- [tarjan](algorithms/graph/tarjan.py)
- [traversal](algorithms/graph/traversal.py)
- [prim](algorithms/graph/prim.py)
- [heap](algorithms/heap)
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
- [skyline](algorithms/heap/skyline.py)
Expand Down
1 change: 1 addition & 0 deletions README_GE.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
- [satisfiability](algorithms/graph/satisfiability.py)
- [tarjan](algorithms/graph/tarjan.py)
- [traversal](algorithms/graph/traversal.py)
- [prim](algorithms/graph/prim.py)
- [heap](algorithms/heap)
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
- [skyline](algorithms/heap/skyline.py)
Expand Down
1 change: 1 addition & 0 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ if __name__ == "__main__":
- [satisfiability](algorithms/graph/satisfiability.py)
- [tarjan](algorithms/graph/tarjan.py)
- [traversal](algorithms/graph/traversal.py)
- [prim](algorithms/graph/prim.py)
- [heap : ヒープ](algorithms/heap)
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
- [skyline](algorithms/heap/skyline.py)
Expand Down
1 change: 1 addition & 0 deletions README_KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ if __name__ == "__main__":
- [satisfiability](algorithms/graph/satisfiability.py)
- [tarjan](algorithms/graph/tarjan.py)
- [traversal](algorithms/graph/traversal.py)
- [prim](algorithms/graph/prim.py)
- [heap : 힙](algorithms/heap)
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
- [skyline](algorithms/heap/skyline.py)
Expand Down
1 change: 1 addition & 0 deletions README_PTBR.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Se você deseja desinstalar os algoritmos, é tão simples quanto:
- [satisfiability](algorithms/graph/satisfiability.py)
- [tarjan](algorithms/graph/tarjan.py)
- [traversal](algorithms/graph/traversal.py)
- [prim](algorithms/graph/prim.py)
- [heap](algorithms/heap)
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
- [skyline](algorithms/heap/skyline.py)
Expand Down
82 changes: 82 additions & 0 deletions algorithms/graph/prim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
Prim's Algorithm.
Determines the minimum spanning tree(MST) of a graph using the Prim's Algorithm
Create a list to store x the vertices.
G = [vertex(n) for n in range(x)]
For each vertex in G, add the neighbors:
G[x].addNeighbor(G[y])
G[y].addNeighbor(G[x])
For each vertex in G, add the edges:
G[x].addEdge(G[y], w)
G[y].addEdge(G[x], w)
To solve run:
MST = prim(G, G[0])
"""

import math


class vertex():
"""Class Vertex."""

def __init__(self, id):
"""
Arguments:
id - input an id to identify the vertex
Attributes:
neighbors - a list of the vertices it is linked to
edges - a dict to store the edges's weight
"""
self.id = str(id)
self.key = None
self.pi = None
self.neighbors = []
self.edges = {} # [vertex:distance]

def __lt__(self, other):
"""Comparison rule to < operator."""
return (self.key < other.key)

def __repr__(self):
"""Return the vertex id."""
return self.id

def addNeighbor(self, vertex):
"""Add a pointer to a vertex at neighbor's list."""
self.neighbors.append(vertex)

def addEdge(self, vertex, weight):
"""Destination vertex and weight."""
self.edges[vertex.id] = weight


def prim(graph, root):
"""
Prim's Algorithm.
Return a list with the edges of a Minimum Spanning Tree
prim(graph, graph[0])
"""
A = []
for u in graph:
u.key = math.inf
u.pi = None
root.key = 0
Q = graph[:]
while Q:
u = min(Q)
Q.remove(u)
for v in u.neighbors:
if (v in Q) and (u.edges[v.id] < v.key):
v.pi = u
v.key = u.edges[v.id]
for i in range(1, len(graph)):
A.append([graph[i].id, graph[i].pi.id])
return(A)

0 comments on commit 59eba87

Please sign in to comment.