Documentation
Documentation
Group 8
Prolog Woldia City Navigation System
• 1. Edge Definitions:
• define the weight(distance) in km
• 2. edge(adago, gonderber, 5).
• 3. edge(gonderber, menharya, 2).
• 4. edge(menharya, piasa, 6).
• 5. edge(piasa, hospital, 3).
• 6. edge(hospital, adago, 4).
• 7. edge(adago, medhanialem, 1).
• 8. edge(mogad, adago, 2).
• 9. edge(adago, menharya, 3).
• 10. edge(menharya, mebrathayl, 2).
• 11. edge(adago, maksegnogebeya, 2).
• o These lines define the roads (edges) between cities with a distance (weight)
associated with each road.
• o For example, the road from adago to gonderber has a distance of 5 KM.
Prolog Woldia City Navigation System
• 12. Bidirectional Connection Rule:
• 13. connected(X, Y, W) :- edge(X, Y, W).
• 14. connected(X, Y, W) :- edge(Y, X, W).
• o This ensures the roads are bidirectional. If there is a road from X
to Y with weight W, then Y to X is also connected with the same weight.
• 15. Dijkstra’s Algorithm – Shortest Path:
• 16. shortest_path(Start, End, Path, Distance) :-
• 17. dijkstra([[0, Start, []]], End, [], Path, Distance).
• o This is the main predicate that finds the shortest path between
Start and End.
• o It calls dijkstra/4 with an initial queue containing a distance of 0,
starting node Start, and an empty path list.
Prolog Woldia City Navigation System
• 18. Base Case of Dijkstra’s Algorithm:
• 19. dijkstra([[Distance, End, Path] | _], End, _, Path, Distance).
• o This is the base case for Dijkstra’s algorithm. If the destination (End) is reached, it returns the
accumulated Path and Distance.
• 20. Recursive Case of Dijkstra’s Algorithm:
• 21. dijkstra([[CurrentDist, CurrentNode, CurrentPath] | RestQueue], End, Visited, Path, Distance) :-
• 22. CurrentNode \== End,
• 23. findall(
• 24. [NewDist, NextNode, [NextNode | CurrentPath]],
• 25. (connected(CurrentNode, NextNode, W),
• 26. \+ member(NextNode, Visited),
• 27. NewDist is CurrentDist + W),
• 28. NextPaths
• 29. ),
• 30. append(RestQueue, NextPaths, NewQueue),
• 31. sort(NewQueue, SortedQueue),
• 32. dijkstra(SortedQueue, End, [CurrentNode | Visited], Path, Distance).
Prolog Woldia City Navigation System
• o This is the recursive case. It explores all the
neighbors (NextNode) of the CurrentNode (the node
currently being processed).
• o For each neighbor, it computes the new distance
(NewDist) and adds the neighbor to the path.
• o It adds the new paths to the queue (RestQueue),
sorts it (to prioritize the shortest paths), and continues
recursively.
Prolog Woldia City Navigation System
• 33. Find All Paths Between Two Cities:
• 34. all_paths(Start, End, Paths) :-
• 35. findall(Path, path(Start, End, [Start], Path), Paths).
• o This predicate finds all possible paths from Start to End.
• o It calls the path/4 predicate to generate each path.
• 36. Path Construction:
• 37. path(Node, Node, Acc, Path) :-
• 38. reverse(Acc, Path).
• o This is the base case for constructing a path. If the current node
is the same as the destination, it reverses the accumulated path
(Acc) to get the correct order from Start to End.
Prolog Woldia City Navigation System
• 44. Calculate the Distance of a Path:
• 45. path_distance([_], 0).
• o This is the base case for calculating the distance of a path. If the path
contains just one node (the destination), the distance is 0.
• 46. Recursive Path Distance Calculation:
• 47. path_distance([A, B | Rest], Distance) :-
• 48. connected(A, B, W),
• 49. path_distance([B | Rest], RestDistance),
• 50. Distance is W + RestDistance.
• • This is the recursive case. It calculates the distance between each pair of
consecutive nodes (A and B) and adds it to the distance of the remaining path
(RestDistance).
• • The total Distance is the sum of the current edge weight (W) and the distance
of the rest of the path.
Prolog Woldia City Navigation System
• . G = nx.Graph()
• • Explanation: Creates an empty undirected graph G using NetworkX. This
graph will store nodes and edges that represent cities and their connections.
• 5. def load_network():
• • Explanation: Defines a function load_network() to populate the graph with
nodes and weighted edges representing different cities and connections between
them.
• 6-25. G.add_edge(...) statements inside load_network()
• • Explanation: Each G.add_edge() adds an edge between two cities
(represented as strings) in the graph. The weight argument defines the weight of
the edge, which could represent the distance or cost between two cities. The
add_edge function takes:
• o Two nodes (cities) as arguments.
• o A weight argument to define the cost/distance between them.
PYTHON CODE