Exemple #1
0
        /// <summary>
        /// Join 2 graphs with undirected edges
        /// </summary>
        /// <returns>The union.</returns>
        /// <param name="with">With.</param>
        public virtual void UndirectedUnion(Graph <CONTENT> with)
        {
            // Add nodes to the graph
            var newNodes = new List <Node <CONTENT> >();

            with.BFS(node =>
            {
                if (null == Find(node.Content))
                {
                    AddNode(node);
                    newNodes.Add(node);
                }
            });

            var edges = with.UndirectedEdges();

            // Cleanup residual edges in the nodes we freshly inserted
            foreach (var newNode in newNodes)
            {
                var neighbors = new List <Node <CONTENT> >();
                newNode.ForEachNeighbor(neighbor => neighbors.Add(neighbor));
                foreach (var neighbor in neighbors)
                {
                    newNode.RemoveDirectedEdge(neighbor);
                }
            }

            foreach (var edge in edges)
            {
                var node     = Find(edge.Item1.Content);
                var neighbor = Find(edge.Item2.Content);
                if (!node.HasNeighbor(neighbor))
                {
                    AddUndirectedEdge(between: node, and: neighbor);
                }
            }
        }
Exemple #2
0
 virtual protected void keyb(byte key, int x, int y)
 {
     if (key == (byte)'c')
     {
         algorithm = null;
         algStep   = 0;
     }
     else if (key == (byte)'1')
     {
         int s = Menu.chooseStart();
         try
         {
             Console.WriteLine(g.BFS(s, out algorithm));
             algStep = 0;
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
     else if (key == (byte)'2')
     {
         Console.WriteLine(g.DFS(out algorithm));
         algStep = 0;
     }
     else if (key == (byte)'3')
     {
         Console.WriteLine(g.MSTKruskal(out algorithm));
         algStep = 0;
     }
     else if (key == (byte)'4')
     {
         int s = Menu.chooseStart();
         try
         {
             Console.WriteLine(g.MSTPrim(s, out algorithm));
             algStep = 0;
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
     else if (key == (byte)'5')
     {
         int s = Menu.chooseStart();
         try
         {
             Console.WriteLine(g.BellmanFord(s, out algorithm));
             algStep = 0;
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
     else if (key == (byte)'6')
     {
         int s = Menu.chooseStart();
         try
         {
             Console.WriteLine(g.Dijkstra(s, out algorithm));
             algStep = 0;
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
     else if (key == (byte)'7')
     {
         Console.WriteLine(g.FloydWarshall(out algorithm));
         algStep = 0;
     }
     else if (key == (byte)'8')
     {
         Console.WriteLine(g.Johnson(out algorithm));
         algStep = 0;
     }
     else if (key == (byte)'9')
     {
         int s = Menu.chooseStart();
         int f = Menu.chooseFinish();
         try
         {
             Console.WriteLine(g.FordFalkerson(s, f, out algorithm));
             algStep = 0;
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
     else if (key == (byte)'0')
     {
         int s = Menu.chooseStart();
         int f = Menu.chooseFinish();
         try
         {
             Console.WriteLine(g.EdmondsKarp(s, f, out algorithm));
             algStep = 0;
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
     else if (key == (byte)'n')
     {
         g         = new Graph(true);
         algorithm = null;
         algStep   = 0;
     }
     else if (key == (byte)'o')
     {
         g.IsOr    = !g.IsOr;
         algorithm = null;
         algStep   = 0;
     }
     else if (key == (byte)'w')
     {
         showWeight = !showWeight;
     }
     Glut.glutPostRedisplay();
 }