Skip to content

Commit 5a0e80d

Browse files
committed
organizing project
1 parent 2f194c5 commit 5a0e80d

File tree

7 files changed

+238
-0
lines changed

7 files changed

+238
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from vertex import Vertex
2+
3+
class Graph:
4+
def __init__(self, size: int = 10):
5+
self.vertices: list = []
6+
self.adjacencyMatrix = [[float("inf") for x in range(size)] for y in range(size)]
7+
self.adjacencyList: dict = {}
8+
self.prev: dict = {}
9+
self.visited: dict = {}
10+
11+
def addVertex(self, label: str, weight:int = float("inf")):
12+
vertex: Vertex = Vertex(label, weight)
13+
vertex.index = len(self.vertices)
14+
self.vertices.append(vertex)
15+
self.prev[label] = None
16+
self.adjacencyList[label] = []
17+
self.visited[label] = False
18+
19+
def addEdge(self, label1: str, label2: str, weight: int):
20+
index1: int = self.findIndexByLabel(label1)
21+
index2: int = self.findIndexByLabel(label2)
22+
self.adjacencyMatrix[index1][index2] = weight
23+
24+
def dijkstra(self, label: str):
25+
index: int = self.findIndexByLabel(label)
26+
current: Vertex = self.vertices[index]
27+
current.weight = 0
28+
while current is not None:
29+
for i in range(len(self.adjacencyMatrix[current.index])):
30+
if self.adjacencyMatrix[current.index][i] != float("inf"):
31+
if current.weight + self.adjacencyMatrix[current.index][i] < self.vertices[i].weight:
32+
self.vertices[i].weight = current.weight + self.adjacencyMatrix[current.index][i]
33+
self.prev[self.vertices[i].label] = current.label
34+
self.visited[current.label] = True
35+
current: Vertex = self.findCheapestVertex()
36+
37+
def findCheapestVertex(self)->Vertex:
38+
current: Vertex = None
39+
for vertex in self.vertices:
40+
if self.visited[vertex.label] == False:
41+
if current is None:
42+
current = vertex
43+
elif current.weight > vertex.weight:
44+
current = vertex
45+
return current
46+
47+
def showPath(self, label: str)->str:
48+
if self.prev[label] is None:
49+
return label
50+
else:
51+
return self.showPath(self.prev[label]) + " -> " + label
52+
53+
def findIndexByLabel(self, label: str)->int:
54+
found: bool = False
55+
count: int = 0
56+
while count < len(self.vertices) and not found:
57+
if self.vertices[count].label == label:
58+
found = True
59+
else:
60+
count += 1
61+
if found:
62+
return count
63+
else:
64+
return None
65+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from graph import Graph
2+
3+
graph = Graph()
4+
5+
graph.addVertex("START")
6+
graph.addVertex("A")
7+
graph.addVertex("C")
8+
graph.addVertex("B")
9+
graph.addVertex("D")
10+
graph.addVertex("END")
11+
12+
graph.addEdge("START", "A", 0)
13+
graph.addEdge("START", "C", 2)
14+
graph.addEdge("A", "B", 18)
15+
graph.addEdge("A", "D", 15)
16+
graph.addEdge("C", "B", 3)
17+
graph.addEdge("C", "D", 10)
18+
graph.addEdge("B", "END", 150)
19+
graph.addEdge("D", "END", 15)
20+
graph.dijkstra("START")
21+
22+
print(graph.showPath("END"))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
class Vertex:
3+
def __init__(self, label: str = None, weight: int = None, index: int = None, key: int = None):
4+
self.label = label
5+
self.weight = weight
6+
self.index = index
7+
self.key = key
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from vertex import Vertex
2+
from priorityqueue import PriorityQueue
3+
4+
class Graph:
5+
def __init__(self):
6+
self.vertices: list = []
7+
self.adjacencyList: dict = {}
8+
self.prev: dict = {}
9+
10+
def addVertex(self, label: str, weight:int = float("inf")):
11+
vertex: Vertex = Vertex(label, weight)
12+
vertex.index = len(self.vertices)
13+
self.vertices.append(vertex)
14+
self.prev[label] = None
15+
self.adjacencyList[label] = []
16+
17+
def addEdge(self, label1: str, label2: str, weight: int):
18+
vertex: Vertex = Vertex(label2, weight)
19+
index: int = self.findIndexByLabel(label2)
20+
vertex.index = index
21+
self.adjacencyList[label1].append(vertex)
22+
23+
def dijkstra(self, label: str):
24+
index: int = self.findIndexByLabel(label)
25+
self.vertices[index].weight = 0
26+
pq = PriorityQueue()
27+
pq.buildHeap(self.vertices)
28+
current: Vertex
29+
while not pq.isEmpty():
30+
current = pq.deleteMin()
31+
for neighbour in self.adjacencyList[current.label]:
32+
if current.weight + neighbour.weight < self.vertices[neighbour.index].weight:
33+
self.prev[self.vertices[neighbour.index].label] = current.label
34+
self.vertices[neighbour.index].weight = current.weight + neighbour.weight
35+
pq.decreaseKey(self.vertices[neighbour.index].key)
36+
37+
def showPath(self, label: str)->str:
38+
if self.prev[label] is None:
39+
return label
40+
else:
41+
return self.showPath(self.prev[label]) + " -> " + label
42+
43+
def findIndexByLabel(self, label: str)->int:
44+
found: bool = False
45+
count: int = 0
46+
while count < len(self.vertices) and not found:
47+
if self.vertices[count].label == label:
48+
found = True
49+
else:
50+
count += 1
51+
if found:
52+
return count
53+
else:
54+
return None
55+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from graph import Graph
2+
3+
graph = Graph()
4+
5+
graph.addVertex("START")
6+
graph.addVertex("A")
7+
graph.addVertex("C")
8+
graph.addVertex("B")
9+
graph.addVertex("D")
10+
graph.addVertex("END")
11+
12+
graph.addEdge("START", "A", 0)
13+
graph.addEdge("START", "C", 2)
14+
graph.addEdge("A", "B", 18)
15+
graph.addEdge("A", "D", 15)
16+
graph.addEdge("C", "B", 3)
17+
graph.addEdge("C", "D", 10)
18+
graph.addEdge("B", "END", 150)
19+
graph.addEdge("D", "END", 15)
20+
graph.dijkstra("START")
21+
22+
print(graph.showPath("END"))
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from vertex import Vertex
2+
3+
class PriorityQueue:
4+
def __init__(self, size: int = 0):
5+
self.heap: list = [None]
6+
self.size = size
7+
8+
def isEmpty(self)->bool:
9+
return self.size == 0
10+
11+
def insert(self, vertex: Vertex):
12+
self.size += 1
13+
vertex.key = self.size
14+
self.heap.append(vertex)
15+
self.percUp(self.size)
16+
17+
def percUp(self, index: int):
18+
while index // 2 > 0:
19+
if self.heap[index].weight < self.heap[index//2].weight:
20+
self.heap[index], self.heap[index//2] = self.heap[index//2], self.heap[index]
21+
self.heap[index].key = index
22+
self.heap[index//2].key = index // 2
23+
index = index // 2
24+
25+
def decreaseKey(self, index):
26+
self.percUp(index)
27+
28+
def deleteMin(self)->Vertex:
29+
if self.isEmpty():
30+
return None
31+
else:
32+
tmp: Vertex = self.heap[1]
33+
self.heap[1] = self.heap[self.size]
34+
self.heap[1].key = 1
35+
self.heap.pop()
36+
self.size -= 1
37+
self.percDown(1)
38+
return tmp
39+
40+
def percDown(self, index: int):
41+
while index * 2 <= self.size:
42+
minIndex: int = self.getMinIndex(index)
43+
if self.heap[index].weight > self.heap[minIndex].weight:
44+
self.heap[index], self.heap[minIndex] = self.heap[minIndex], self.heap[index]
45+
self.heap[index].key = index
46+
self.heap[minIndex].key = minIndex
47+
index = minIndex
48+
49+
def getMinIndex(self, index: int):
50+
if index * 2 + 1 > self.size:
51+
return index * 2
52+
else:
53+
if self.heap[index * 2].weight <= self.heap[index * 2 + 1].weight:
54+
return index * 2
55+
else:
56+
return index * 2 + 1
57+
58+
def buildHeap(self, vertices: list):
59+
for vertex in vertices:
60+
self.insert(vertex)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
class Vertex:
3+
def __init__(self, label: str = None, weight: int = None, index: int = None, key: int = None):
4+
self.label = label
5+
self.weight = weight
6+
self.index = index
7+
self.key = key

0 commit comments

Comments
 (0)