Finding a route from one city to another
destwhich are substances of class
Kfor source node, and
Vfor destination node.
addNodeto add Node object as the
addEdgeto add Edge object, which will append the
destproperty of Edge object to
edgeswith the specific
chirdrenOfto return all the child nodes
getNodeby node’s name
Graph - is a subclass of Digraph
addEdge: rewrite the
addEdgemethod to add another set with reversed source and destination nodes.
Similar to left-first depth-first method of enumerating a search tree, mainly difference is that graph might have cycles, so we must keep track of what nodes we have visited
def DFS(graph, start, end, path, shortest, toPrint):
Explore the nodes layer by layer, like first we got a node as start node, then check all the children nodes, then children’s children, it will keep going until we got the node equal the end node. And that’s the path we are looking for, just return it.
""" Explore all paths with n hops
digraph ['daiɡrɑ:f, -ɡræf] n. 有向图