Implemented dijkstra algorithm with priority queue in binary heap.
This commit is contained in:
parent
10437a49c3
commit
7310699ea8
0
algorithms/data_structures.py
Normal file
0
algorithms/data_structures.py
Normal file
@ -1,6 +1,78 @@
|
||||
def dijkstra_algorithm(graph, s ):
|
||||
print()
|
||||
import heapq as heap
|
||||
|
||||
|
||||
# function which takes graph, vertex set, start and goal, uses djikstra algorithm to establish shortest paths from start
|
||||
# every other vertex in graph and returns shortest path between start and goal
|
||||
def dijkstra_algorithm(graph, point_set, start, goal):
|
||||
# dictionary which will store for every vertex it's distance to start
|
||||
dist = dict()
|
||||
|
||||
# dictionary which keeps if vertex was already popped from queue
|
||||
visited = dict()
|
||||
|
||||
# dictionary which keeps for every vertex which other vertex is previous on the shortest path from start
|
||||
prev = dict()
|
||||
|
||||
# list which is used to keep priority queue with vertexes to be explored by the algorithm
|
||||
queue = []
|
||||
|
||||
# creating dictionary which for every vertex keeps all it's neighbors
|
||||
for edge in graph.keys():
|
||||
point_set[edge[0]].append(edge[1])
|
||||
point_set[edge[1]].append(edge[0])
|
||||
|
||||
for vertex in point_set:
|
||||
dist[vertex] = float('inf')
|
||||
prev[vertex] = None
|
||||
visited[vertex] = False
|
||||
|
||||
dist[start] = 0
|
||||
heap.heappush(queue, (0, start))
|
||||
visited[start] = True
|
||||
|
||||
while len(queue) > 0:
|
||||
current = heap.heappop(queue)
|
||||
for neighbor in point_set[current[1]]:
|
||||
new_dist = 0
|
||||
if neighbor > current[1]:
|
||||
new_dist = dist[current[1]] + graph[(neighbor, current[1])]
|
||||
else:
|
||||
new_dist = dist[current[1]] + graph[(current[1], neighbor)]
|
||||
|
||||
if new_dist < dist[neighbor]:
|
||||
dist[neighbor] = new_dist
|
||||
prev[neighbor] = current[1]
|
||||
|
||||
if not visited[neighbor]:
|
||||
visited[neighbor] = True
|
||||
heap.heappush(queue, (new_dist, neighbor))
|
||||
temp = goal
|
||||
shortest_path = [goal]
|
||||
while prev[temp] is not None:
|
||||
shortest_path.append(prev[temp])
|
||||
temp = prev[temp]
|
||||
return shortest_path, dist[goal]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print()
|
||||
g = {
|
||||
(2, 1): 3,
|
||||
(3, 2): 2,
|
||||
(5, 3): 1,
|
||||
(9, 5): 5,
|
||||
(10, 9): 4,
|
||||
(9, 4): 3,
|
||||
(4, 1): 4,
|
||||
(7, 1): 6,
|
||||
(3, 1): 4,
|
||||
(6, 2): 3,
|
||||
(8, 6): 8,
|
||||
(8, 3): 2
|
||||
}
|
||||
|
||||
v = dict()
|
||||
for i in g.keys():
|
||||
v[i[0]] = []
|
||||
v[i[1]] = []
|
||||
|
||||
print(dijkstra_algorithm(g, v, 7, 10))
|
||||
|
File diff suppressed because it is too large
Load Diff
10
main.py
10
main.py
@ -1,5 +1,11 @@
|
||||
import tools.file_service as op_file
|
||||
import algorithms.dijkstra as dijkstra
|
||||
|
||||
if __name__ == '__main__':
|
||||
e = op_file.read_graph_from_file("dataset/deezer_clean_data/HR_edges.csv")
|
||||
op_file.add_weight_to_file(e, "dataset/deezer_clean_data/HR_edges", ".csv")
|
||||
g = op_file.read_graph_from_file("dataset/deezer_clean_data/HR_edgeswith_weight.csv", has_weight=True)
|
||||
v = dict()
|
||||
for i in g.keys():
|
||||
v[i[0]] = []
|
||||
v[i[1]] = []
|
||||
|
||||
print(dijkstra.dijkstra_algorithm(g, v, 0, 4))
|
||||
|
@ -9,14 +9,16 @@ def read_graph_from_file(path, separator=",", read_first_line=False, is_directed
|
||||
while line != '':
|
||||
if read_line:
|
||||
if line[0] != "#":
|
||||
split = line.split(separator)
|
||||
split = line.strip('\n').split(separator)
|
||||
node1 = int(split[0])
|
||||
node2 = int(split[1])
|
||||
if not is_directed:
|
||||
if node1 < node2:
|
||||
node1, node2 = node2, node1
|
||||
if not has_weight:
|
||||
edges[(node1, node2)] = 1
|
||||
else:
|
||||
edges[(node1, node2)] = split[2]
|
||||
edges[(node1, node2)] = int(split[2])
|
||||
else:
|
||||
read_line = True
|
||||
line = file.readline()
|
||||
@ -24,7 +26,7 @@ def read_graph_from_file(path, separator=",", read_first_line=False, is_directed
|
||||
|
||||
|
||||
def add_weight_to_file(graph, path, extension, separator=",", weight_scale=(1, 100)):
|
||||
with open(path + "with_weight" + extension, 'w') as file:
|
||||
with open(path + "_with_weight" + extension, 'w') as file:
|
||||
for key in graph.keys():
|
||||
file.write(str(key[0]) + separator + str(key[1]) + separator + str(rand.randint(weight_scale[0],
|
||||
weight_scale[1])) + "\n")
|
||||
|
Loading…
Reference in New Issue
Block a user