public Edge FindEdge(Node startNode, Node endNode) { foreach (Edge edge in Edges) { if ((edge.StartNode.Name == startNode.Name) && (edge.EndNode.Name == endNode.Name)) { return edge; } } return null; }
// Time complexity ? public List<Node> FindShortestPath(Node source, Node destination) { if (FindNode(source) == null) { throw new Exception("Source node is not present in the graph"); } if (FindNode(destination) == null) { throw new Exception("destination node is not present in graph"); } UpdateGraphWithDistancesFromNode(source); return FindShortestPath(destination); }
public List<Node> GetNodesExceptGivenOne(Node n) { if (Nodes == null) { throw new Exception("Error occured since Nodes are null"); } List<Node> result = new List<Node>(); foreach (Node node in Nodes) { if (node != n) { result.Add(node); } } return result; }
public bool DoesEdgeExist(Node neighbor) { foreach (Edge edge in Edges) { if ((edge.EndNode == neighbor) || (edge.StartNode == neighbor)) { return true; } } return false; }
static void Main(string[] args) { string[] cityNames = {"Bombay", "Pune", "Goa", "Hydrebad" }; Graph graph = new Graph(); graph.Edges = new List<Edge>(); graph.Nodes = new List<Node>(); // First create nodes foreach (string city in cityNames) { Node node = new Node(city); node.Neighbors = new List<Node>(); node.Edges = new List<Edge>(); node.IsVisited = false; node.Distance = Constants.INFINITY; graph.Nodes.Add(node); } // Then assign neighbors to each node foreach (Node n in graph.Nodes) { n.Neighbors.AddRange(graph.GetNodesExceptGivenOne(n)); } // Then intialize edges graph.IntializeEdges(); // Intialize cost of Edges InitializeEdgeCost(graph, "Bombay", "Pune", 5); InitializeEdgeCost(graph, "Pune", "Bombay", 5); InitializeEdgeCost(graph, "Bombay", "Goa", 3); InitializeEdgeCost(graph, "Goa", "Bombay", 3); InitializeEdgeCost(graph, "Bombay", "Hydrebad", 60); InitializeEdgeCost(graph, "Hydrebad", "Bombay", 60); InitializeEdgeCost(graph, "Pune", "Hydrebad", 30); InitializeEdgeCost(graph, "Hydrebad", "Pune", 30); InitializeEdgeCost(graph, "Goa", "Hydrebad", 10); InitializeEdgeCost(graph, "Hydrebad", "Goa", 10); InitializeEdgeCost(graph, "Pune", "Goa", 25); InitializeEdgeCost(graph, "Goa", "Pune", 25); // Do breadth first traversal // graph.PerformBreadthFirstTraversal(); Node origin = graph.FindNode("Bombay"); if (origin == null) { throw new Exception("Origin is null"); } Node destination = graph.FindNode("Hydrebad"); if (destination == null) { throw new Exception("Destination is null"); } List<Node> shortestPath = graph.FindShortestPath(origin, destination); System.Console.WriteLine("The shortest path from " + origin.Name + " to " + destination.Name); foreach (Node n in shortestPath) { System.Console.WriteLine(n.Name); } }
public Edge(Node startNode, Node endNode) { StartNode = startNode; EndNode = endNode; }
public Node FindNode(Node node) { return FindNode(node.Name); }
void UpdateGraphWithDistancesFromNode(Node source) { PriorityQueue<Node> unVisitedNodes = new PriorityQueue<Node>(Nodes); // Time to create a min heap - O(n) // Does this update the value of 'source' in Nodes ? source.Distance = 0; while (!unVisitedNodes.Empty()) // O(n) { Node current = unVisitedNodes.Peek(); if (current.Distance == Constants.INFINITY) { break; } foreach (Node neighbor in current.Neighbors) // O(nm) { if (unVisitedNodes.Contains(neighbor)) { int tentative = 0; Edge edge = FindEdge(current, neighbor); // O(nml) tentative = current.Distance + edge.Cost; if (tentative < neighbor.Distance) { neighbor.Distance = tentative; neighbor.Previous = current; } } } unVisitedNodes.Dequeue(); } }
List<Node> FindShortestPath(Node destination) { if (destination == null) { throw new Exception("Error occured: Destination is null"); } Node current = destination.Previous; Stack<Node> resultReverse = new Stack<Node>(); resultReverse.Push(destination); while (current != null) { resultReverse.Push(current); current = current.Previous; } List<Node> result = new List<Node>(); while (resultReverse.Count > 0) { result.Add(resultReverse.Pop()); } return result; }