Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            int[,] edges = new int[, ] {
                { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 }, { 5, 6 }, { 6, 3 }, { 6, 0 }, { 6, 1 }, { 1, 3 }
            };
            int[] weights = new int[10] {
                1, 2, 1, 3, 2, 2, 1, 3, 3, 4
            };
            IGraph graph = new GraphAdjacencyList(7);

            for (int i = 0; i < 10; i++)
            {
                graph.AddEdge(graph.GetVertex(edges[i, 0]), graph.GetVertex(edges[i, 1]), weights[i]);
            }

            // IGraph sceleton = GraphWorker.Prim(graph);
            // sceleton.Edges().ForEach(edge =>
            // {
            //     Console.Out.WriteLine(edge.ToString());
            // });


            GraphProps graphProps = GraphWorker.WaveAlgorithm(graph, graph.GetVertex(0));

            Console.Out.WriteLine(graphProps);
            graph.Vertices().ForEach(vertex =>
            {
                Console.Out.WriteLine(vertex.ToString());
            });
        }
Ejemplo n.º 2
0
        public static GraphProps WaveAlgorithm(IGraph graph, Vertex start)
        {
            graph.Vertices().ForEach(vertex => { vertex.Colour = 0; });
            Queue <Vertex> queue = new Queue <Vertex>();

            queue.Enqueue(start);
            GraphProps graphProps = new GraphProps();

            start.Colour = 1;

            while (queue.Count > 0)
            {
                Vertex        temp        = queue.Dequeue();
                List <Vertex> surrounding = graph.Surrounding(temp);
                surrounding.ForEach(vertex =>
                {
                    if (vertex.Colour == 0)
                    {
                        vertex.Colour = temp.Colour + 1;
                        queue.Enqueue(vertex);
                        vertex.ComeFrom = temp;
                    }
                    else if (vertex.Colour % 2 == temp.Colour % 2)
                    {
                        graphProps.IsBipartite = false;
                    }
                });
            }
            graphProps.IsConnected = true;

            foreach (var vertex in graph.Vertices())
            {
                if (vertex.Colour == 0)
                {
                    graphProps.IsConnected = false;
                    break;
                }
            }
            return(graphProps);
        }