Skip to content

Commit 68ac826

Browse files
committed
Added Dijkstra's shortest path algo and hashing implementation
1 parent 451c4a9 commit 68ac826

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

Graph/dijkstra.py

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
2+
# PRONOUNCED AS 'DAAIKSTRA'
3+
4+
class Vertex(object):
5+
def __init__(self, key):
6+
self.id = key
7+
self.connectedTo = {}
8+
9+
def addNeighbor(self, nbr, w):
10+
self.connectedTo[nbr] = w
11+
12+
def getConnections(self):
13+
return self.connectedTo.keys()
14+
15+
def getId(self):
16+
return self.id
17+
18+
def getWeight(self, nbr):
19+
return self.connectedTo[nbr]
20+
21+
def __str__(self):
22+
return str(self.id) + " connected to " + str([x.id for x in self.connectedTo])
23+
24+
class Graph(object):
25+
def __init__(self):
26+
self.vertices = {}
27+
self.numVertices = 0
28+
29+
def addVertex(self, key):
30+
self.numVertices += 1
31+
temp = Vertex(key)
32+
self.vertices[key] = temp
33+
34+
def getVertex(self, key):
35+
if key in self.vertices:
36+
return self.vertices[key]
37+
return None
38+
39+
def addEdge(self, fro, to, cost=0):
40+
if fro not in self.vertices:
41+
self.addVertex(fro)
42+
if to not in self.vertices:
43+
self.addVertex(to)
44+
self.vertices[fro].addNeighbor(self.vertices[to], cost)
45+
46+
def getVertices(self):
47+
return self.vertices.keys()
48+
49+
def __iter__(self):
50+
return iter(self.vertices.values())
51+
52+
def __contains__(self, n):
53+
return n in self.vertices
54+
55+
def disjkstra(self, src, dest):
56+
if dest.id not in self.vertices or src.id not in self.vertices:
57+
return None
58+
dist = {}
59+
for vert in self.vertices.values():
60+
dist[vert] = float('inf')
61+
62+
dist[src] = 0
63+
visited = []
64+
prevVertex = { vertex: None for vertex in self.vertices.values() }
65+
while dest not in visited:
66+
min = float('inf')
67+
x = None
68+
for y in dist:
69+
if dist[y] < min and y not in visited:
70+
min = dist[y]
71+
x = y
72+
73+
if min == float('inf'):
74+
break
75+
visited.append(x)
76+
for nbr in x.connectedTo:
77+
if nbr in visited:
78+
break
79+
if (dist[x] + x.connectedTo[nbr]) < dist[nbr]:
80+
dist[nbr] = dist[x] + x.connectedTo[nbr]
81+
prevVertex[nbr] = x
82+
83+
path, current = [], dest
84+
while prevVertex[current] != None:
85+
path.insert(0, current.id)
86+
current = prevVertex[current]
87+
path.insert(0, src.id)
88+
#return dist[dest]
89+
return path
90+
91+
92+
93+
if __name__ == "__main__":
94+
g = Graph()
95+
for i in range(9):
96+
g.addVertex(i)
97+
98+
g.addEdge(0, 1, 4)
99+
g.addEdge(0, 7, 8)
100+
g.addEdge(1, 2, 8)
101+
g.addEdge(2, 3, 7)
102+
g.addEdge(1, 7, 11)
103+
g.addEdge(7, 8, 7)
104+
g.addEdge(2, 8, 2)
105+
g.addEdge(7, 6, 1)
106+
g.addEdge(8, 6, 6)
107+
g.addEdge(6, 5, 2)
108+
g.addEdge(2, 5, 4)
109+
g.addEdge(3, 5, 14)
110+
g.addEdge(5, 4, 10)
111+
g.addEdge(3, 4, 9)
112+
113+
print(g.disjkstra(g.getVertex(0), g.getVertex(8)))

Searching-and-sorting/hashing.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
class HashTable(object):
3+
def __init__(self, size):
4+
self.size = size
5+
self.slots = [None] * self.size
6+
self.data = [None] * self.size
7+
8+
def put(self, key, data):
9+
hashvalue = self.hashfunction(key, len(self.slots))
10+
11+
if self.slots[hashvalue] == None:
12+
self.slots[hashvalue] = key
13+
self.data[hashvalue] = data
14+
else:
15+
if self.slots[hashvalue] == key:
16+
self.slots[hashvalue] = data
17+
else:
18+
nextslot = self.rehash(hashvalue, len(self.slots))
19+
while self.slots[nextslot] != None and self.slots[nextslot] != key:
20+
nextslot = self.rehash(nextslot, len(self.slots))
21+
if self.slots[nextslot] == None:
22+
self.slots[nextslot] = key
23+
self.data[nextslot] = data
24+
else:
25+
self.data[nextslot] = data
26+
27+
28+
def hashfunction(self, key, size):
29+
return key % size
30+
31+
def rehash(self, oldhash, size):
32+
return (oldhash + 1) % size
33+
34+
def get(self, key):
35+
startslot = self.hashfunction(key, len(self.slots))
36+
data = None
37+
stop = False
38+
found = False
39+
position = startslot
40+
41+
while self.slots[position] != None and not found and not stop:
42+
if self.slots[position] == key:
43+
found = True
44+
data = self.data[position]
45+
46+
else:
47+
position = self.rehash(position, len(self.slots))
48+
if position == startslot:
49+
stop = True
50+
51+
return data
52+
53+
if __name__ == "__main__":
54+
h = HashTable(5)
55+
for i in range(5):
56+
h.put(i, i * 2)
57+
58+
for i in range(5):
59+
print(h.get(i))

0 commit comments

Comments
 (0)