BFS and DFS
This commit is contained in:
73
main.py
Normal file
73
main.py
Normal file
@@ -0,0 +1,73 @@
|
||||
from graph import Graph, Node, Queue
|
||||
from utils import getNode
|
||||
|
||||
# directed and weighted digraph
|
||||
romania = Graph( ['Or', 'Ne', 'Ze', 'Ia', 'Ar', 'Si', 'Fa',
|
||||
'Va', 'Ri', 'Ti', 'Lu', 'Pi', 'Ur', 'Hi',
|
||||
'Me', 'Bu', 'Dr', 'Ef', 'Cr', 'Gi'],
|
||||
[
|
||||
('Or', 'Ze', 71), ('Or', 'Si', 151),
|
||||
('Ne', 'Ia', 87), ('Ze', 'Ar', 75),
|
||||
('Ia', 'Va', 92), ('Ar', 'Si', 140),
|
||||
('Ar', 'Ti', 118), ('Si', 'Fa', 99),
|
||||
('Si', 'Ri', 80), ('Fa', 'Bu', 211),
|
||||
('Va', 'Ur', 142), ('Ri', 'Pi', 97),
|
||||
('Ri', 'Cr', 146), ('Ti', 'Lu', 111),
|
||||
('Lu', 'Me', 70), ('Me', 'Dr', 75),
|
||||
('Dr', 'Cr', 120), ('Cr', 'Pi', 138),
|
||||
('Pi', 'Bu', 101), ('Bu', 'Gi', 90),
|
||||
('Bu', 'Ur', 85), ('Ur', 'Hi', 98),
|
||||
('Hi', 'Ef', 86)
|
||||
] )
|
||||
|
||||
def search(graph, queue, start_node_name, target_node_name):
|
||||
cost = 0
|
||||
visited_nodes = [] # Nodes which have been visited
|
||||
path = []
|
||||
|
||||
start_node = getNode(start_node_name, graph.nodes)
|
||||
target_node = getNode(target_node_name, graph.nodes)
|
||||
|
||||
queue.push(start_node)
|
||||
|
||||
while not queue.empty():
|
||||
current_node = queue.pop()
|
||||
visited_nodes.append(current_node.name)
|
||||
|
||||
if current_node == target_node:
|
||||
path.append(current_node.name)
|
||||
tmp = current_node
|
||||
while not tmp == start_node:
|
||||
for edge in tmp.parent.edges:
|
||||
if edge.end.name == tmp.name:
|
||||
cost += edge.value
|
||||
tmp = tmp.parent
|
||||
path.insert(0, tmp.name) # reversed insertion
|
||||
else:
|
||||
for edge in current_node.edges:
|
||||
neighbor = edge.end
|
||||
|
||||
# works with digraph, because current_node is marked at this point, a cycle is not possible
|
||||
if not visited_nodes.__contains__(neighbor.name):
|
||||
queue.push(neighbor)
|
||||
neighbor.parent = current_node
|
||||
|
||||
if path.__len__() == 0:
|
||||
print('zwischen ' + start_node_name + ' und ' + target_node_name + ' konnte kein Pfad gefunden werden')
|
||||
else:
|
||||
print('From ' + start_node_name + ' to ' + target_node_name + ': ')
|
||||
print('Path: ' + path.__str__().format())
|
||||
print('Cost: ' + cost.__str__())
|
||||
|
||||
def bfs(graph, start_node_name, target_node_name):
|
||||
search(graph,Queue('FIFO'),start_node_name, target_node_name)
|
||||
|
||||
def dfs(graph, start_node_name, target_node_name):
|
||||
search(graph,Queue('LIFO'),start_node_name, target_node_name)
|
||||
|
||||
def main():
|
||||
bfs(romania, 'Ti', 'Bu')
|
||||
dfs(romania, 'Ti', 'Bu')
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user