public static void Solve() { var n = I; var m = I; var l = new int[m]; var r = new int[m]; var d = new long[m]; var gr = new AdjacencyList <long>(n); Repeat(m, i => { l[i] = I - 1; r[i] = I - 1; d[i] = L; gr.Add(l[i], r[i], d[i]); gr.Add(r[i], l[i], -d[i]); }); var x = new long[n]; foreach (var edge in gr.DepthFirstSearch()) { x[edge.Target] = x[edge.Source] + edge.Cost; } Repeat(m, j => { if (x[r[j]] - x[l[j]] != d[j]) { Answer(No); } }); Answer(Yes); }
public static void Solve() { var n = I; var m = I; var l = new int[m]; var r = new int[m]; var d = new long[m]; var gr = new AdjacencyList <long>(n); Repeat(m, i => { l[i] = I - 1; r[i] = I - 1; d[i] = L; gr.Add(l[i], r[i], d[i]); gr.Add(r[i], l[i], -d[i]); }); var visited = Enumerable.Range(0, n).Select(t => !gr[t].Any()).ToArray(); var visitedCnt = visited.Count(t => t); var stack = new Stack <Edge <long> >(gr[0]); var x = Repeat(n, () => - 1L).ToArray(); while (visitedCnt < n) { var i = Enumerable.Range(0, n).First(j => !visited[j]); gr[i].ForEach(stack.Push); while (stack.Any()) { var edge = stack.Pop(); if (!visited[edge.Target]) { x[edge.Target] = x[edge.Source] + edge.Cost; gr[edge.Target].ForEach(stack.Push); visited[edge.Target] = true; visitedCnt++; } } } Repeat(m, j => { if (x[r[j]] - x[l[j]] != d[j]) { Answer(No); } }); Answer(Yes); }
public void AddVertex(string name) { if (list.Contains(name) == false) { list.Add(new Vertex(name)); } }
/// <summary> /// Clones a Node by pointing to the same memebers /// </summary> /// <param name="node"></param> /// public Node(INode <KEY> node) { if (node == null) { return; } _key = node.Key; _data = node.Data; _properties = node.Properties; Node <KEY> nodeT = node as Node <KEY>; if (nodeT != null) { _edges = nodeT._edges; _adjacencyList = nodeT._adjacencyList; } else { foreach (INode <KEY> n in node.AdjacentNodes) { AdjacencyList.Add(n); } foreach (IEdge <KEY> edge in node.EdgeSet) { Edges.Add(new KeyValuePair <EdgeKey <KEY>, IEdge <KEY> >(new EdgeKey <KEY>(edge.SourceNode.Key, edge.TargetNode.Key), edge)); } } }
/// <summary> /// Adds a new Node to graph /// </summary> /// <param name="value">value to add to graph</param> /// <returns>newly created node containing value</returns> public Vertex <T> AddNode(T value) { Vertex <T> node = new Vertex <T>(value); AdjacencyList.Add(node, new List <Edge <T> >()); size++; return(node); }
public void AddToAdjacencyList(Tile t) { if (t == null) { return; } if (AdjacencyList == null) { AdjacencyList = new List <Tile>(); } AdjacencyList.Add(t); }
public void buildAdjacencyList(int[][] edges) { //for(var adLs in graph.AdjacencyList) for (int i = 0; i < Verticies; i++) { AdjacencyList.Add(i, new List <int>()); } for (int i = 0; i < edges.Length; i++) { addEdge(edges[i][0], edges[i][1]); } }
public void AddNode(AbstractAdjacentGraphNode <TNode> graphNode) { if (graphNode == null) { throw new ArgumentNullException("graphNode"); } if (DoesNodeExist(graphNode.Node)) { throw new DuplicateNodeException(string.Format("The node {0} already exists and cannot be added!", graphNode.Node)); } AdjacencyList.Add(graphNode); }
/// <summary> /// /// </summary> /// <param name="n"></param> public IEdge <KEY> AddAdjacentNode(INode <KEY> n) { if (n == null) { throw new ArgumentNullException("n"); } IEdge <KEY> e = new Edge <KEY>(this, n); Edges.Add(new EdgeKey <KEY>(this.Key, n.Key), e); AdjacencyList.Add(n); return(e); }
public Graph AddNode(LocationNode node) { if (node == null) { return(this); } var contains = AdjacencyList.Any(list => list.Contains(node)); if (contains) { return(this); } AdjacencyList.Add(new List <LocationNode> { node }); return(this); }
/// <summary> /// /// </summary> /// <param name="n"></param> /// <param name="e"></param> /// <returns></returns> public IEdge <KEY> AddAdjacentNode(INode <KEY> n, IEdge <KEY> e) { if (n == null) { throw new ArgumentNullException("n"); } if (e == null) { throw new ArgumentNullException("e"); } e.SourceNode = this; e.TargetNode = n; Edges.Add(new EdgeKey <KEY>(this.Key, n.Key), e); AdjacencyList.Add(n); return(e); }
private static void Dikstra() { var directedGraph = new AdjacencyList(5); directedGraph.Add(1, 2, 1); directedGraph.Add(1, 1, 4); // Dijkstra accepts cycles, But not negative weights // directedGraph.Add(2, 3, 5); directedGraph.Add(3, 4, 10); directedGraph.Add(4, 0, 2); var dijskra = new Dijkstra(); dijskra.Compute(directedGraph, 1); for (int i = 0; i < dijskra.Shortest.Length; i++) Console.WriteLine("\t {0} + -> Weights : [{1}]", i, dijskra.Shortest[i]); Console.WriteLine("\nPredessors: "); for (int i = 0; i < dijskra.Predecessors.Length; i++) { var pred = dijskra.Predecessors[i]; Console.WriteLine("pred[{0}] : {1}", i, pred != null ? pred.ToString() : "NULL"); } }
/// <summary> /// Adds an edge based on the data in the passed-in EdgeToNeighbor instance. /// </summary> protected internal virtual void AddDirected(EdgeToNeighbor e) { neighbors.Add(e); }
public void AddToAdjList(HiddenMarkovState State) { AdjacencyList.Add(State); }
public void AddToAdjacencyList(Node node) { AdjacencyList.Add(node); }
public Graph() { var n0 = new Node(300, 300); var n1 = new Node("FFFF"); var n2 = new Node("7834"); var n3 = new Node("2834"); //var n0 = new Node(124); //Console.WriteLine("An element ID: {0}", n0.ID); //var n1 = new Node(254); //Console.WriteLine("An element ID: {0}", n1.ID); //var n2 = new Node(172); //Console.WriteLine("An element ID: {0}", n2.ID); //var n3 = new Node(94); //Console.WriteLine("An element ID: {0}", n3.ID); //var n4 = new Node(47); //Console.WriteLine("An element ID: {0}", n4.ID); ////var n5 = new Node(49); ////Console.WriteLine("An element ID: {0}", n5.ID); //var n5 = new Node(2); //Console.WriteLine("An element ID: {0}", n5.ID); VertexList.Add(n0); VertexList.Add(n1); VertexList.Add(n2); VertexList.Add(n3); //VertexList.Add(n4); //VertexList.Add(n5); //Console.WriteLine(); for (int i = 1; i < 51; i++) { var nn = new Node(i * 6 - 4); Console.WriteLine("An element ID: {0}", nn.ID); VertexList.Add(nn); } //Console.WriteLine(); //Generate(VertexList.Count, 2); //AddEdge(0, 3); //AddEdge(1, 3); //AddEdge(2, 3); //AddEdge(3, 4); //AddEdge(4, 2); //AddEdge(0, 1); //AddEdge(0, 5); for (int i = 0; i < EdgeList.Count; i++) { AdjacencyList.Add(EdgeList[i].v1); HL.Add(-1); } for (int i = EdgeList.Count - 1; i >= 0; i--) { AdjacencyList.Add(EdgeList[i].v2); } for (int k = 0; k < AdjacencyList.Count; k++) { int i = AdjacencyList[k]; LL.Add(HL[i]); HL[i] = k; } this.GraphWrite(); //Console.WriteLine("Обход списка рёбер вершины 2"); //int v = 2; //for(int k = HL[v]; k!=-1; k = LL[k]) //{ // int j = AdjacencyList[AdjacencyList.Count - 1-k]; // Console.WriteLine("{0} - {1}", v, j); //} //foreach (Node v in VertexList) //{ // Console.WriteLine("\n\n\nNode IP: {0}, Node ID: {1}", v.ip, v.ID); // v.WriteNeighboorhoodSet(); // int pref = v.ComparePrefixes(v.ID, VertexList[0].ID); // // Console.WriteLine("\nPrefixes ID1: {0}, ID2: {1}, {2}", v.ID, VertexList[0].ID, pref); // v.WriteLeafSet(); // v.WriteRoutingTable(); //} }
public static void Dag() { var dag = new AdjacencyMatrix(5); dag.Add(0, 2); dag.Add(1, 4); dag.Add(6, 3); // Bad Input .. nothing // dag.Add(3, 4); Console.WriteLine("\n " + dag); Console.WriteLine("Topological Sort result :"); var l = DagAlgorithms.TopologicalSort(dag); l.ForEach((i) => Console.WriteLine(i)); var dag2 = new AdjacencyList(5); dag2.Add(0, 2, 1); dag2.Add(1, 4, 10); dag2.Add(2, 3, 5); dag2.Add(3, 1, 2); dag2.Add(3, 4, 1); Console.WriteLine(dag2); Console.WriteLine("\t Vertices: " + dag2.N + "\n\t Edges: " + dag2.M); Console.WriteLine("\nTopological Sort result :"); var l1 = DagAlgorithms.TopologicalSort(dag2); l1.ForEach((i) => Console.WriteLine(i)); // test removing an edge .. // dag2.Remove(1, 4); Console.WriteLine(dag2); Console.WriteLine("\nTopological Sort result :"); var l2 = DagAlgorithms.TopologicalSort(dag2); l2.ForEach((i) => Console.WriteLine(i)); Console.WriteLine("Shortest Path: "); var dagAlgo = new DagAlgorithms(); dagAlgo.ShortestPath(dag2, 0); for (int i = 0; i < dagAlgo.Shortest.Length; i++) Console.WriteLine("\t {0} + -> Weights : [{1}]", i, dagAlgo.Shortest[i]); Console.WriteLine("\nPredessors: "); for (int i = 0; i < dagAlgo.Predecessors.Length; i++) { var pred = dagAlgo.Predecessors[i]; Console.WriteLine("pred[{0}] : {1}", i, pred != null ? pred.ToString() : "NULL"); } var dagTime = new AdjacencyList(5); // Or AdjacencyMatrix(5) // dagTime.Add(1, 2, 2.5); dagTime.Add(1, 3, 5); dagTime.Add(3, 4, 3.25); //dagTime.Add(4, 0, null); // if e.g.: Unreachable in P time // dagTime.Add(4, 0, 10.5); Console.WriteLine(dagTime); Console.WriteLine("\nShortest Path: "); var dagAlgo2 = new DagAlgorithms(); dagAlgo2.ShortestPath(dagTime, 1); for (int i = 0; i < dagAlgo2.Shortest.Length; i++) Console.WriteLine("\t {0} + -> Weights : [{1}]", i, dagAlgo2.Shortest[i]); Console.WriteLine("\nPredessors: "); for (int i = 0; i < dagAlgo2.Predecessors.Length; i++) { var pred = dagAlgo2.Predecessors[i]; Console.WriteLine("pred[{0}] : {1}", i, pred != null ? pred.ToString() : "NULL"); } }
public Vertex AddVertex(Vertex vertex) { AdjacencyList.Add(vertex, new List <Vertex>()); //return the added node return(Vertex); }
public void Add_Vertex_NoThrow() { Assert.DoesNotThrow(() => { adjList.Add(new VertexNode("A")); }); }
protected virtual internal void AddLink(Link l) { neighbors.Add(l); }
private static void BellmanFord() { var directedGraph = new AdjacencyList(5); directedGraph.Add(1, 2, 1); directedGraph.Add(1, 1, 4); // bellman-ford accepts cycles, And negative weights too. // directedGraph.Add(2, 3, -5); directedGraph.Add(3, 4, -2); directedGraph.Add(4, 0, 2); directedGraph.Add(4, 3, -2); var bF = new BellmanFord(); bF.Compute(directedGraph, 1); for (int i = 0; i < bF.Shortest.Length; i++) Console.WriteLine("\t {0} + -> Weights : [{1}]", i, bF.Shortest[i]); Console.WriteLine("\nPredessors: "); for (int i = 0; i < bF.Predecessors.Length; i++) { var pred = bF.Predecessors[i]; Console.WriteLine("pred[{0}] : {1}", i, pred != null ? pred.ToString() : "NULL"); } Console.WriteLine("Negative Cycle :"); bF.FindNegativeWeightCycle(directedGraph).ForEach(i => Console.Write(i + " -> ")); Console.WriteLine(); }
/// <summary> /// Adds a new Adjacency Row to the Graph object and increments its count /// </summary> /// <param name="vertex">The vertex that will be the key for that adjacency row</param> private void AddNewAdjacencyRow(Vertex vertex) { AdjacencyList.Add(new AdjacencyRow(vertex)); Count += 1; }