Esempio n. 1
0
        //We parametrize our insertion with the directed Boolean flag
        // This is to identify whether we need to insert two copies of each edge or only one.
        // Because Edges by themselves are always directional in Adjacency List
        // So if (x,y) is an edge, in Undirected we need to add (y,x) too
        private void InsertEdge(int x, int y, bool directed)
        {
            // create a node | We represent | Directed Edge (x, y) by an EdgeNode y in x's Adjacency List
            EdgeNode edgeNode = new EdgeNode
            {
                // point the new Node to Edges[x]'s linked list
                Next   = Edges[x],
                YValue = y,
                Weight = null,

                XThatComesFromEdgeIndexHereForDebugging = x
            };

            // Put the new node at the HEAD of the Adjacency Linked list
            Edges[x] = edgeNode;

            // Increase the degree
            Degree[x] = Degree[x] + 1;
            NEdges++;

            if (!directed)
            {
                // The same edge has to appear in Y's Adjacency List | Thus graph is Undirected
                InsertEdge(y, x, true);
            }
        }
Esempio n. 2
0
 public void PrintGraph()
 {
     for (int i = 0; i < NVertices; i++)
     {
         Console.WriteLine($"Vertex:{i}");;
         EdgeNode edge = this.Edges[i];
         while (edge != null)
         {
             Console.WriteLine($"{edge.YValue}");
             edge = edge.Next;
         }
     }
 }
Esempio n. 3
0
        public void PrintGraphBetter()
        {
            Console.WriteLine("-- START --");
            for (int i = 0; i < NVertices; i++)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append($"Vertex:{i} ->");

                EdgeNode edge = this.Edges[i];
                while (edge != null)
                {
                    builder.Append($"{edge.YValue}");
                    if (edge.Next != null)
                    {
                        builder.Append(" ->");
                    }
                    edge = edge.Next;
                }

                Console.WriteLine(builder.ToString());
            }
            Console.WriteLine("-- END --");
        }
Esempio n. 4
0
 public Graph(int MAXV)
 {
     Edges  = new EdgeNode[MAXV];
     Degree = new int[MAXV];
     MaxV   = MAXV;
 }