Solution: Remove an Edge
In this review lesson, we will give a detailed analysis of the solution to remove an edge from a directed graph.
We'll cover the following...
Solution: using graph traversal
def remove_edge(graph, source, destination):"""A function to remove an edge:param graph: A graph:param source: Source Vertex:param destination: Destination Vertex"""if graph.V == 0:return graphif source >= graph.V or source < 0:return graphif destination >= graph.V or destination < 0:return graphtemp = graph.graph[source]# If head node itself holds the key to be deletedif temp is not None:if temp.vertex == destination:graph.graph[source] = temp.nexttemp = Nonereturnwhile temp is not None:if destination == temp.vertex:breakprev = temptemp = temp.nextif temp is None:return# Set the new link# from removed node's previous node to next nodeprev.next = temp.nexttemp = None# Main program to test above functionif __name__ == "__main__":g = Graph(5)g.add_edge(0, 1)g.add_edge(0, 2)g.add_edge(1, 3)g.add_edge(1, 4)g.add_edge(2, 4)print("Before: ")g.print_graph()remove_edge(g, 1, 3)print("After: ")g.print_graph()
Explanation
This ...
Ask