public void GetEdgesFrom_ShouldReturnAllEdgesStartingFromStartVertex() { var startVertex = 'C'; var expected = new List <DirectedEdge> { new DirectedEdge(startVertex, 'D', 6) , new DirectedEdge(startVertex, 'E', 8) , new DirectedEdge(startVertex, 'A', 1) }; var target = new AdjacencyList(); foreach (var edge in expected) { target.AddDirectedEdge(edge); } var actual = target.GetEdgesFrom(startVertex); for (var i = 0; i < actual.Count; i++) { Assert.That(actual[i].StartVertex, Is.EqualTo(expected[i].StartVertex)); Assert.That(actual[i].EndVertex, Is.EqualTo(expected[i].EndVertex)); Assert.That(actual[i].Weight, Is.EqualTo(expected[i].Weight)); } }
private static AdjacencyList <TNode, TWeight> EdgeContraction <TNode, TWeight>(AdjacencyList <TNode, TWeight> graph, Edge <TNode, TWeight> edge) { TNode first = edge.Node; TNode second = edge.Connection.Node; AdjacencyList <TNode, TWeight> result = new AdjacencyList <TNode, TWeight>(); foreach (TNode node in graph) { if (object.Equals(node, second) || object.Equals(node, first)) { LinkedList <Connection <TNode, TWeight> > connections = graph.GetConnections(node); foreach (Connection <TNode, TWeight> connection in connections) { if (!object.Equals(connection.Node, first) && !object.Equals(connection.Node, second)) { result.AddDirectedEdge(first, connection.Node, connection.Weight); } } } else { LinkedList <Connection <TNode, TWeight> > connections = graph.GetConnections(node); foreach (Connection <TNode, TWeight> connection in connections) { if (object.Equals(connection.Node, second)) { result.AddDirectedEdge(node, first, connection.Weight); } else { result.AddDirectedEdge(node, connection.Node, connection.Weight); } } } } return(result); }
public BreadthFirstSearch(AdjacencyList <TNode, TWeight> graph, TNode startNode) { if (!graph.HasTheNode(startNode)) { throw new NotImplementedException(); } this.startNode = startNode; searchResult = new AdjacencyList <TNode, TWeight>(); this.labels = new SortedList <TNode, BreadthFirstSearchNode <TNode> >(graph.NumberOfNodes); foreach (TNode node in graph) { this.searchResult.AddNode(node); labels.Add(node, new BreadthFirstSearchNode <TNode>(NodeStatus.White, double.PositiveInfinity)); } labels[startNode].Status = NodeStatus.Gray; labels[startNode].Value = 0; //.Status = NodeStatus.Gray; labels[startNode].Value = 0; Queue <TNode> nodes = new Queue <TNode>(); nodes.Enqueue(startNode); while (nodes.Count > 0) { TNode currentNode = nodes.Dequeue(); labels[currentNode].Status = NodeStatus.Black; foreach (Connection <TNode, TWeight> item in graph.GetConnections(currentNode)) { if (labels[item.Node].Status == NodeStatus.White) { nodes.Enqueue(item.Node); labels[item.Node].Status = NodeStatus.Gray; labels[item.Node].Predecessor = labels[currentNode]; labels[item.Node].Value = labels[currentNode].Value + 1; searchResult.AddDirectedEdge(currentNode, item.Node, item.Weight); } } } }
public void AddDirectedEdge_ShouldAddVerticesWithUppercase() { var startVertex = 'a'; var endVertex = 'b'; var weight = 4; var target = new AdjacencyList(); target.AddDirectedEdge(startVertex, endVertex, weight); var actual = target.GetEdgesFrom(startVertex); Assert.That(actual.Exists(anEdge => anEdge.StartVertex == char.ToUpperInvariant(startVertex) && anEdge.EndVertex == char.ToUpperInvariant(endVertex) && anEdge.Weight == weight)); }
public void AddDirectedEdge_ShouldAddEdgeToTheStartVertex() { var startVertex = 'A'; var endVertex = 'C'; var weight = 7; var target = new AdjacencyList(); target.AddDirectedEdge(startVertex, endVertex, weight); var actual = target.GetEdgesFrom(startVertex); Assert.That(actual.Exists(anEdge => anEdge.StartVertex == startVertex && anEdge.EndVertex == endVertex && anEdge.Weight == weight)); }
private void Visit(TNode currentNode, ref int time) { //this.labels[currentNode].Status = NodeStatus.Gray; //time += 1; //this.labels[currentNode].Value = new TimeStamp(time, null); this.labels[currentNode].DiscoverTime = ++time; LinkedList <Connection <TNode, TWeight> > connections = graph.GetConnections(currentNode); foreach (Connection <TNode, TWeight> node in connections) { if (this.labels[node.Node].IsWhite()) { searchResult.AddDirectedEdge(currentNode, node.Node, node.Weight); labels[node.Node].Predecessor = labels[currentNode]; Visit(node.Node, ref time); } else if (this.labels[node.Node].IsGray()) { this.backEdges.Add(new Edge <TNode, TWeight>(currentNode, new Connection <TNode, TWeight>(node.Node, node.Weight))); } else if (this.labels[node.Node].IsBlack()) { List <TNode> temp = GetPathToSource(node.Node); if (temp.Contains(currentNode)) { this.forwardEdges.Add(new Edge <TNode, TWeight>(currentNode, new Connection <TNode, TWeight>(node.Node, node.Weight))); } else { this.crossEdges.Add(new Edge <TNode, TWeight>(currentNode, new Connection <TNode, TWeight>(node.Node, node.Weight))); } } else { throw new NotImplementedException(); } } //this.labels[currentNode].Status = NodeStatus.Black; //this.labels[currentNode].Value = new TimeStamp(this.labels[currentNode].Value.DiscoverTime, ++time); this.labels[currentNode].FinishTime = ++time; }
public AdjacencyList <TNode, TWeight> GetDepthFirstSearchGraph() { AdjacencyList <TNode, TWeight> result = new AdjacencyList <TNode, TWeight>(this.NumberOfNodes); foreach (TNode item in this.labels.Keys) { result.AddNode(item); } for (int i = 0; i < this.NumberOfNodes; i++) { if (this.labels.Values[i].Predecessor != null) { result.AddDirectedEdge(this.labels.Values[i].Predecessor.Value, this.labels.Values[i].Value, new TWeight()); } } return(result); }
public void GetEdgesFrom_ShouldIgnoreCaseAndReturnAllEdgesFromTheStartVertex() { var startVertex = 'd'; var expected = new List <DirectedEdge> { new DirectedEdge(startVertex, 'g', 4) , new DirectedEdge(startVertex, 'E', 2) }; var target = new AdjacencyList(); foreach (var edge in expected) { target.AddDirectedEdge(edge); } var actual = target.GetEdgesFrom(startVertex); Assert.That(actual.Count, Is.EqualTo(expected.Count)); }
public void AddOneWayPath(char source, char destination, int length) { _adjacencyList.AddDirectedEdge(source, destination, length); }
public static AdjacencyList <TNode, TWeight> CalculateByKruskal <TNode, TWeight>(AdjacencyList <TNode, TWeight> graph) where TWeight : IComparable { List <Triple <LinkedListNode <TNode>, TWeight, LinkedListNode <TNode> > > list = new List <Triple <LinkedListNode <TNode>, TWeight, LinkedListNode <TNode> > >(); // what is the use of jungle? List <LinkedList <TNode> > jungle = new List <LinkedList <TNode> >(); Dictionary <TNode, LinkedListNode <TNode> > nodeList = new Dictionary <TNode, LinkedListNode <TNode> >(); AdjacencyList <TNode, TWeight> result = new AdjacencyList <TNode, TWeight>(); foreach (TNode item in graph) { LinkedList <TNode> temp = new LinkedList <TNode>(); temp.AddFirst(item); nodeList.Add(item, temp.First); jungle.Add(temp); } for (int i = 0; i < graph.NumberOfNodes; i++) { foreach (Connection <TNode, TWeight> item in graph.GetConnections(graph[i])) { list.Add(new Triple <LinkedListNode <TNode>, TWeight, LinkedListNode <TNode> >(nodeList[graph[i]], item.Weight, nodeList[item.Node])); } } list.Sort(new Comparison <Triple <LinkedListNode <TNode>, TWeight, LinkedListNode <TNode> > >(CompareEdge)); foreach (Triple <LinkedListNode <TNode>, TWeight, LinkedListNode <TNode> > item in list) { LinkedListNode <TNode> first = item.First; LinkedListNode <TNode> second = item.Third; if (first.List != second.List) { result.AddDirectedEdge(first.Value, second.Value, item.Second); result.AddDirectedEdge(second.Value, first.Value, item.Second); LinkedList <TNode> tempList = second.List; do { LinkedListNode <TNode> tempNode = tempList.First; tempList.RemoveFirst(); first.List.AddLast(tempNode); } while (tempList.Count > 0); jungle.Remove(tempList); } } return(result); }