GraphEdge

We found 10 examples in language CSharp for this search. You will see 41 fragments of code.

	// Add an edge (but don't add duplicate edges)
        public void AddIncidentEdge(GraphEdge e)
        {
	    foreach (GraphEdge edge in incidentEdges)
	    {
	        if (edge.ToString() == e.ToString()) 
		    return;
	    }
            incidentEdges.Add(e);
        }

        // return a list of all outgoing edges
        public List<GraphEdge> GetEdges()
        {
            return incidentEdges;
        }

        // return a list of outgoing edges of specified Label
        public List<GraphEdge> GetEdges(string label)
        {
            List<GraphEdge> list = new List<GraphEdge>();
            foreach (GraphEdge e in incidentEdges)
                if (e.Label == label)
                    list.Add(e);
            return list;
        }

        // return text form of node, including outgoing edges
        public override string ToString()
        {
            string result = Name + "\n";
            foreach (GraphEdge e in incidentEdges)
            {
                result = result + "  " + e.ToString() + "\n";
            }
            return result;
        }

        // return a list of all nodes connected to this node.
        public List<GraphNode> ToNodes()
        {
            List<GraphNode> ret = new List<GraphNode>();
            foreach (GraphEdge e in incidentEdges)
                ret.Add(e.ToNode());

            return ret;
        }

            static void Main(string[] args)
            {
                GraphEdge graphEdge = new GraphEdge();
                var edges = graphEdge.dataset.Select(e => new GraphEdge()
                {
                    Source = e.SourceMachine.Trim(),
                    Target = e.TargetMachine.Trim(),
                    Weight = e.Connection
                }).ToList();
     
            }


        // Add an edge (but don't add duplicate edges)
        public void AddIncidentEdge(GraphEdge e) {
            foreach (GraphEdge edge in incidentEdges) {
                if (edge.ToString() == e.ToString())
                    return;
            }
            incidentEdges.Add(e);
        }

        // return a list of all outgoing edges
        public List<GraphEdge> GetEdges() {
            return incidentEdges;
        }

        // return a list of outgoing edges of specified Label
        public List<GraphEdge> GetEdges(string label) {
            List<GraphEdge> list = new List<GraphEdge>();
            foreach (GraphEdge e in incidentEdges)
                if (e.Label == label)
                    list.Add(e);
            return list;
        }

        // return text form of node, including outgoing edges
        public override string ToString() {
            string result = Name + "\n";
            result += "  label: " + Label + "\n";
            foreach (GraphEdge e in incidentEdges) {
                result = result + "  " + e.ToString() + "\n";
            }
            return result;
        }


	// Add an edge (but don't add duplicate edges)
        public void AddIncidentEdge(GraphEdge e)
        {
	    foreach (GraphEdge edge in incidentEdges)
	    {
	        if (edge.ToString() == e.ToString()) 
		    return;
	    }
            incidentEdges.Add(e);
        }

        // return a list of all outgoing edges
        public List<GraphEdge> GetEdges()
        {
            return incidentEdges;
        }

        // return a list of outgoing edges of specified label
        public List<GraphEdge> GetEdges(string label)
        {
            List<GraphEdge> list = new List<GraphEdge>();
            foreach (GraphEdge e in incidentEdges)
                if (e.Label() == label)
                    list.Add(e);
            return list;
        }

        // return text form of node, including outgoing edges
        public override string ToString()
        {
            string result = name + "\n";
            foreach (GraphEdge e in incidentEdges)
            {
                result = result + "  " + e.ToString() + "\n";
            }
            return result;
        }


	// Add an edge (but don't add duplicate edges)
        public void AddIncidentEdge(GraphEdge e)
        {
	    foreach (GraphEdge edge in incidentEdges)
	    {
	        if (edge.ToString() == e.ToString()) 
		    return;
	    }
            incidentEdges.Add(e);
        }

        // return a list of all outgoing edges
        public List<GraphEdge> GetEdges()
        {
            return incidentEdges;
        }

        // return a list of outgoing edges of specified label
        public List<GraphEdge> GetEdges(string label)
        {
            List<GraphEdge> list = new List<GraphEdge>();
            foreach (GraphEdge e in incidentEdges)
                if (e.Label() == label)
                    list.Add(e);
            return list;
        }

        // return text form of node, including outgoing edges
        public override string ToString()
        {
            string result = name + "\n";
            foreach (GraphEdge e in incidentEdges)
            {
                result = result + "  " + e.ToString() + "\n";
            }
            return result;
        }


	// Add an edge (but don't add duplicate edges)
        public void AddIncidentEdge(GraphEdge e)
        {
	    foreach (GraphEdge edge in incidentEdges)
	    {
	        if (edge.ToString() == e.ToString()) 
		    return;
	    }
            incidentEdges.Add(e);
        }

        // return a list of all outgoing edges
        public List<GraphEdge> GetEdges()
        {
            return incidentEdges;
        }

        // return a list of outgoing edges of specified Label
        public List<GraphEdge> GetEdges(string label)
        {
            List<GraphEdge> list = new List<GraphEdge>();
            foreach (GraphEdge e in incidentEdges)
                if (e.Label == label)
                    list.Add(e);
            return list;
        }

        public List<GraphNode> ToNodes()
        {
            List<GraphNode> tn = new List<GraphNode>();
            foreach (GraphEdge n in incidentEdges)
                tn.Add(n.ToNode());
            return tn;
        }

        // return text form of node, including outgoing edges
        public override string ToString()
        {
            string result = Name + "\n";
            foreach (GraphEdge e in incidentEdges)
            {
                result = result + "  " + e.ToString() + "\n";
            }
            return result;
        } 

        /// <summary>
        /// Tests the correctness of shortest distance computation. Root is node A. 
        /// To visualize the graph see: <img src = "../Images/Graphs/Disjkstra.png"/>.
        /// </summary>
        [TestMethod]
        public void GetShortestDistancesFromRoot()
        {
            var A = new GraphNode<string>("A");
            var B = new GraphNode<string>("B");
            var C = new GraphNode<string>("C");
            var D = new GraphNode<string>("D");
            var E = new GraphNode<string>("E");
            var F = new GraphNode<string>("F");
            var G = new GraphNode<string>("G");
            var H = new GraphNode<string>("H");
            var I = new GraphNode<string>("I");

            A.Adjacents.Add(new GraphEdge<string>(B, 4));
            A.Adjacents.Add(new GraphEdge<string>(C, 8));

            B.Adjacents.Add(new GraphEdge<string>(A, 4));
            B.Adjacents.Add(new GraphEdge<string>(C, 11));
            B.Adjacents.Add(new GraphEdge<string>(D, 8));

            C.Adjacents.Add(new GraphEdge<string>(A, 8));
            C.Adjacents.Add(new GraphEdge<string>(B, 11));
            C.Adjacents.Add(new GraphEdge<string>(E, 7));
            C.Adjacents.Add(new GraphEdge<string>(F, 1));

            D.Adjacents.Add(new GraphEdge<string>(B, 8));
            D.Adjacents.Add(new GraphEdge<string>(E, 2));
            D.Adjacents.Add(new GraphEdge<string>(G, 7));
            D.Adjacents.Add(new GraphEdge<string>(H, 4));

            E.Adjacents.Add(new GraphEdge<string>(D, 2));
            E.Adjacents.Add(new GraphEdge<string>(C, 7));
            E.Adjacents.Add(new GraphEdge<string>(F, 6));

            F.Adjacents.Add(new GraphEdge<string>(C, 1));
            F.Adjacents.Add(new GraphEdge<string>(E, 6));
            F.Adjacents.Add(new GraphEdge<string>(H, 2));

            G.Adjacents.Add(new GraphEdge<string>(D, 7));
            G.Adjacents.Add(new GraphEdge<string>(H, 14));
            G.Adjacents.Add(new GraphEdge<string>(I, 9));

            H.Adjacents.Add(new GraphEdge<string>(F, 2));
            H.Adjacents.Add(new GraphEdge<string>(D, 4));
            H.Adjacents.Add(new GraphEdge<string>(G, 14));
            H.Adjacents.Add(new GraphEdge<string>(I, 10));

            I.Adjacents.Add(new GraphEdge<string>(G, 9));
            I.Adjacents.Add(new GraphEdge<string>(H, 10));

            List<GraphNode<string>> distancesFromRoot = Dijkstra.GetShortestDistancesFromRoot(A);

            Assert.AreEqual("A", distancesFromRoot[0].Value);
            Assert.AreEqual(0, distancesFromRoot[0].DistanceFromStartNode);

            Assert.AreEqual("B", distancesFromRoot[1].Value);
            Assert.AreEqual(4, distancesFromRoot[1].DistanceFromStartNode);

            Assert.AreEqual("C", distancesFromRoot[2].Value);
            Assert.AreEqual(8, distancesFromRoot[2].DistanceFromStartNode);

            Assert.AreEqual("F", distancesFromRoot[3].Value);
            Assert.AreEqual(9, distancesFromRoot[3].DistanceFromStartNode);

            Assert.AreEqual("H", distancesFromRoot[4].Value);
            Assert.AreEqual(11, distancesFromRoot[4].DistanceFromStartNode);

            Assert.AreEqual("D", distancesFromRoot[5].Value);
            Assert.AreEqual(12, distancesFromRoot[5].DistanceFromStartNode);

            Assert.AreEqual("E", distancesFromRoot[6].Value);
            Assert.AreEqual(14, distancesFromRoot[6].DistanceFromStartNode);

            Assert.AreEqual("G", distancesFromRoot[7].Value);
            Assert.AreEqual(19, distancesFromRoot[7].DistanceFromStartNode);

            Assert.AreEqual("I", distancesFromRoot[8].Value);
            Assert.AreEqual(21, distancesFromRoot[8].DistanceFromStartNode);
        }


        /// <summary>
        /// Generates the grid graph.
        /// </summary>
        /// <param name="m">The m.</param>
        /// <param name="n">The n.</param>
        /// <param name="spacing">The spacing.</param>
        /// <param name="centerOffset">The center offset.</param>
        /// <returns></returns>
        public static SparseGraph GenerateGridGraph(int m, int n, float spacing, Vector3 centerOffset)
        {
            GraphNode[,] nodes = GenerateGraphNodes(n, m, spacing, centerOffset);
            // Copy over to the sparse graph.
            SparseGraph sparseGraph = new SparseGraph(false);
            for (int i = 0; i < m; ++i)
            {
                for (int j = 0; j < n; ++j)
                    sparseGraph.Nodes.Add(nodes[i, j]);
            }

            List<GraphEdge> edges = GenerateGraphEdges(nodes, m, n);

            sparseGraph.Edges.AddRange(edges);

            return sparseGraph;
        }

        /// <summary>
        /// Adds the edge.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <returns></returns>
        public bool AddEdge(int from, int to)
        {
            return AddEdge(from, to, 1.0f);
        }

        /// <summary>
        /// Adds the edge.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <param name="cost">The cost.</param>
        /// <returns></returns>
        public bool AddEdge(int from, int to, float cost)
        {
            return AddEdge(new GraphEdge(from, to, cost));
        }

        /// <summary>
        /// Adds the edge.
        /// </summary>
        /// <param name="edge">The edge.</param>
        /// <returns></returns>
        public bool AddEdge(GraphEdge edge)
        {
            bool copyExists = false;
            foreach (GraphEdge cEdge in _edges)
            {
                if (cEdge == edge)
                    copyExists = true;
            }
            if (!copyExists)
                _edges.Add(edge);

            return copyExists;
        }

        /// <summary>
        /// Adds the edges for rendering.
        /// </summary>
        /// <param name="primitivesRenderer">The primitives renderer.</param>
        /// <param name="renderColor">Color of the render.</param>
        /// <param name="selectedRenderColor">Color of the selected render.</param>
        /// <param name="selectedIndicies">The selected indicies.</param>
        public void AddEdgesForRendering(BaseLogic.Graphics.PrimitivesRenderer primitivesRenderer,
            Microsoft.Xna.Framework.Color renderColor, Microsoft.Xna.Framework.Color selectedRenderColor, List<int> selectedIndicies)
        {
            for (int i = 0; i < _edges.Count; ++i)
            {
                GraphEdge edge = _edges[i];

                GraphNode node1 = GetNode(edge.To);
                GraphNode node2 = GetNode(edge.From);

                if (selectedIndicies.Contains(i))
                    primitivesRenderer.AddRenderLine(node1.Position, node2.Position, selectedRenderColor);
                else
                    primitivesRenderer.AddRenderLine(node1.Position, node2.Position, renderColor);
            }
        }

        /// <summary>
        /// Adds the node.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <param name="z">The z.</param>
        /// <returns></returns>
        public int AddNode(float x, float y, float z)
        {
            return AddNode(new Microsoft.Xna.Framework.Vector3(x, y, z));
        }

        public static void  Driver()
        {
            //New Graph

            List<GraphEdge> graphEdges = new List<GraphEdge>()
            {
                new GraphEdge(0, 1, 6),
                new GraphEdge(1, 2, 7),
                new GraphEdge(2, 0, 5),
                new GraphEdge(2, 1, 4),
                new GraphEdge(3, 2, 10),
                new GraphEdge(4, 5, 1)
            };

            var graph = new GraphDataStructure(graphEdges);

            graph.AddEdge(new GraphEdge(5, 4, 3));
            graph.PrintGraph();        

            /*
            Graph graph = new Graph(7);
            graph.AddEdge(graph, 0, 1);
            graph.AddEdge(graph, 0, 4);
            graph.AddEdge(graph, 1, 4);
            graph.AddEdge(graph, 1, 2);
            graph.AddEdge(graph, 1, 3);
            graph.AddEdge(graph, 2, 3);
            graph.AddEdge(graph, 3, 4);

            graph.PrintGraph(graph);
            */
        }

        //Undirected graph, so add froom source to dest and vice versa
        public void AddEdge(Graph graph, int source, int destinaion)
        {
            graph.adjArrayList[source].AddLast(destinaion);
            graph.adjArrayList[destinaion].AddFirst(source);
        }

        public void PrintGraph(Graph graph)
        {
            for (int v = 0; v < graph.vertices; v++)
            {
                Console.WriteLine();
                Console.WriteLine($"Adjacency list of vertex {v}");
                Console.Write("Head");
                foreach (var num in graph.adjArrayList[v])
                    Console.Write($"-->{num}");
                Console.WriteLine();
            }
        }

        public void AddEdge(GraphEdge graphEdge)
        {
            if (AdjacenceyList[graphEdge.Source] == null)
                AdjacenceyList[graphEdge.Source] = new List<GraphNode>();

            AdjacenceyList[graphEdge.Source].Add(new GraphNode(graphEdge.Destination, graphEdge.Cost));
        }

        public void RemoveEdge(GraphEdge graphEdge)
        {
            if (AdjacenceyList[graphEdge.Source] == null)
                throw new Exception("Such edge does not exists");
            var removalNode = AdjacenceyList[graphEdge.Source].Find((x) => (x.Destination == graphEdge.Destination) && (x.Cost == graphEdge.Cost));
            AdjacenceyList[graphEdge.Source].Remove(removalNode);
        }

        public void PrintGraph()
        {
            foreach(var node in AdjacenceyList)
            {
                foreach(var innerNode in node)
                {
                    Console.Write($"{node} {innerNode.Destination} => {innerNode.Cost}");
                }
                Console.WriteLine();
            }
        }


        public bool Add(GraphEdge graphEdge)
        {
            graphEdges.Add(graphEdge);
            return true;
        }

        public bool Contains(object @object)
        {
            if (graphEdges == null)
                return false;

            if (@object is GraphEdge)
                return graphEdges.Contains((GraphEdge)@object);

            foreach (GraphEdge graphEdge in graphEdges)
                if (graphEdge.Object == @object)
                    return true;

            return false;
        }

        public bool Remove(object @object)
        {
            if (@object is GraphEdge)
                return graphEdges.Remove((GraphEdge)@object);

            return graphEdges.RemoveAll(x => x.Object == @object) > 0;
        }

        public IEnumerator<GraphEdge> GetEnumerator()
        {
            return graphEdges.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return graphEdges.GetEnumerator();
        }

NuGet.DependencyResolver.GraphEdge<TItem> : Object

Constructors :

public GraphEdge(GraphEdge outerEdge = , GraphItem<TItem> item = , LibraryDependency edge = )

Methods :

public GraphEdge get_OuterEdge()
public GraphItem<TItem> get_Item()
public LibraryDependency get_Edge()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods