Esempio n. 1
0
        private static void TestBFS()
        {
            Graph g = new Graph();
            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();
            Vertex v7 = new Vertex();

            g.AddVertex(v1);
            g.AddVertex(v2);
            g.AddVertex(v3);
            g.AddVertex(v4);
            g.AddVertex(v5);
            g.AddVertex(v6);
            g.AddVertex(v7);

            g.AddEdge(new Edge(v1, v2));
            g.AddEdge(new Edge(v1, v3));
            g.AddEdge(new Edge(v2, v6));
            g.AddEdge(new Edge(v2, v4));
            g.AddEdge(new Edge(v6, v4));
            g.AddEdge(new Edge(v4, v5));

            List<Edge> path = GraphAlgorithms.BreadthFirstSearch(g, 0, 4);
        }
Esempio n. 2
0
 public Vertex AddVertex()
 {
     checkNullGraph(currentGraph);
     Vertex v = new Vertex();
     currentGraph.AddVertex(v);
     return v;
 }
Esempio n. 3
0
 public FlowEdge AddFlowEdge(Vertex from, Vertex to)
 {
     checkNullGraph(currentGraph);
     FlowEdge fe = new FlowEdge(from, to, 0, 1f);
     currentGraph.AddEdge(fe);
     return fe;
 }
Esempio n. 4
0
 public FlowEdge(Vertex from, Vertex to, float currentFlow, float capacity)
     : base(from, to)
 {
     Directed = true;
     CurrentFlow = currentFlow;
     Capacity = capacity;
 }
Esempio n. 5
0
        public Edge AddEdge(Vertex from, Vertex to)
        {
            checkNullGraph(currentGraph);

            Edge e = new Edge(from, to, 1f);

            currentGraph.AddEdge(e);
            return e;
        }
Esempio n. 6
0
        private static void TestAllPairs()
        {
            //Graph for figure 4.15 part B
            Graph g = new Graph();
            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();
            Vertex v7 = new Vertex();
            Vertex v8 = new Vertex();
            Vertex v9 = new Vertex();
            Vertex v10 = new Vertex();
            Vertex v11 = new Vertex();
            Vertex v12 = new Vertex();
            g.AddVertex(v1);
            g.AddVertex(v2);
            g.AddVertex(v3);
            g.AddVertex(v4);
            g.AddVertex(v5);
            g.AddVertex(v6);
            g.AddVertex(v7);
            g.AddVertex(v8);
            g.AddVertex(v9);
            g.AddVertex(v10);
            g.AddVertex(v11);
            g.AddVertex(v12);

            g.AddEdge(new Edge(v1, v2, 5));
            g.AddEdge(new Edge(v1, v4, 10));
            g.AddEdge(new Edge(v2, v3, 7));
            g.AddEdge(new Edge(v2, v5, 1));
            g.AddEdge(new Edge(v3, v6, 4));
            g.AddEdge(new Edge(v4, v5, 3));
            g.AddEdge(new Edge(v5, v6, 3));
            g.AddEdge(new Edge(v1, v2, 5));
            g.AddEdge(new Edge(v4, v7, 11));
            g.AddEdge(new Edge(v1, v2, 5));
            g.AddEdge(new Edge(v5, v8, 7));
            g.AddEdge(new Edge(v6, v9, 5));
            g.AddEdge(new Edge(v7, v10, 9));
            g.AddEdge(new Edge(v7, v8, 2));
            g.AddEdge(new Edge(v8, v11, 1));
            g.AddEdge(new Edge(v8, v9, 0));
            g.AddEdge(new Edge(v9, v12, 12));
            g.AddEdge(new Edge(v11, v12, 4));

            float[,] d = GraphAlgorithms.AllPairsShortestPath(g);

            GraphAlgorithms.PrintMatrix(d, g.GetVertices().Count);
        }
Esempio n. 7
0
        public static Graph GenerateRandomGraph(int n)
        {
            float prob = 0.2f;

            Graph g = new Graph();
            g.Directed = false;

            for (int i = 0; i < n; i++)
            {
                Vertex v = new Vertex();
                g.AddVertex(v);
            }

            Random r = new Random();

            //Randomly decide when to add an edge between any two pairs
            foreach (Vertex v in g.GetVertices())
            {
                foreach (Vertex u in g.GetVertices())
                {
                    if (!v.Equals(u))
                    {
                        double random = r.NextDouble();
                        if (random >= 0 && random <= prob)
                        {
                            g.AddEdge(new Edge(v, u, (float)r.NextDouble()));
                        }
                    }
                }
            }

            foreach (Vertex v in g.GetVertices())
            {
                VertexClassification vc = ClassifyVertex(v);
                Output.WriteLine("Vertex " + v.ToString() + " is " + vc.ToString());
            }

            return g;
        }
Esempio n. 8
0
        public static VertexClassification ClassifyVertex(Vertex v)
        {
            int myDegree = v.GetTotalDegree();

            List<Vertex> verts = v.GetAllNeighbors();

            bool regular = true;
            foreach (Vertex u in verts)
            {
                if (myDegree != u.GetTotalDegree())
                {
                    regular = false;
                    break;
                }
            }

            if (regular)
            {
                return VertexClassification.Regular;
            }

            bool veryStrong = true;
            foreach (Vertex u in verts)
            {
                if (myDegree <= u.GetTotalDegree())
                {
                    veryStrong = false;
                    break;
                }
            }

            if (veryStrong)
            {
                return VertexClassification.VeryStrong;
            }

            bool strong = true;
            foreach (Vertex u in verts)
            {
                if (myDegree < u.GetTotalDegree())
                {
                    strong = false;
                    break;
                }
            }

            if (strong)
            {
                return VertexClassification.Strong;
            }

            bool veryWeak = true;
            foreach (Vertex u in verts)
            {
                if (myDegree >= u.GetTotalDegree())
                {
                    veryWeak = false;
                    break;
                }
            }

            if (veryWeak)
            {
                return VertexClassification.VeryWeak;
            }

            bool weak = true;
            foreach (Vertex u in verts)
            {
                if (myDegree > u.GetTotalDegree())
                {
                    weak = false;
                    break;
                }
            }

            if (weak)
            {
                return VertexClassification.Weak;
            }

            return VertexClassification.Typical;
        }
Esempio n. 9
0
 public void SetSource(Vertex s)
 {
     source = s;
 }
Esempio n. 10
0
        private void copyLinkedGraph(Graph to)
        {
            to.Clear();
            to.Directed = this.Directed;

            for (int i = 0; i < this.GetVertices().Count; i++)
            {
                vertices[i].Tag = i;
                Vertex v = new Vertex();
                v.Tag = i;
                to.AddVertex(v);
            }

            for (int i = this.GetEdges().Count-1; i >= 0; i--)
            {
                Edge currentEdge = this.GetEdges()[i];

                Vertex v1 = new Vertex();
                v1.Label = currentEdge.GetFromVertex().Label;
                int cd = (int)currentEdge.GetFromVertex().Tag;
                if ((int)to.GetVertices()[cd].Tag == cd)
                {
                    v1 = to.GetVertices()[cd];
                }

                Vertex v2 = new Vertex();
                v2.Label = currentEdge.GetToVertex().Label;
                int cd2 = (int)currentEdge.GetToVertex().Tag;
                if ((int)to.GetVertices()[cd2].Tag == cd2)
                {
                    v2 = to.GetVertices()[cd2];
                }

                Edge e = currentEdge;
                e = new Edge(v1, v2, currentEdge.Weight);

                to.AddEdge(e);
            }
        }
Esempio n. 11
0
 public virtual void AddVertex(Vertex v)
 {
     v.SetGraph(this);
     vertexCount++;
     v.Label = vertexCount.ToString();
     vertices.Add(v);
     matrix.AddVertex();
 }
Esempio n. 12
0
        private static void TestLabelCorrecting()
        {
            //Graph for figure 5.10 part A
            Graph g = new Graph();
            g.Directed = true;
            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();

            g.AddEdge(new Edge(v1, v2, 10));
            g.AddEdge(new Edge(v1, v3, 15));
            g.AddEdge(new Edge(v2, v3, 25));
            g.AddEdge(new Edge(v3, v2, -20));
            g.AddEdge(new Edge(v2, v4, 0));
            g.AddEdge(new Edge(v2, v5, 5));
            g.AddEdge(new Edge(v4, v5, -5));
            g.AddEdge(new Edge(v5, v4, 10));
            g.AddEdge(new Edge(v5, v3, 30));

            Console.WriteLine("Label Correcting Output:");
            Graph labelCorrect = GraphAlgorithms.LabelCorrecting(g, 0);
        }
Esempio n. 13
0
        private static void TestDominatingSets()
        {
            Graph g = new Graph();
            g.Directed = false;

            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();
            Vertex v7 = new Vertex();

            g.AddEdge(new Edge(v1, v2));
            g.AddEdge(new Edge(v2, v3));
            g.AddEdge(new Edge(v3, v4));
            g.AddEdge(new Edge(v4, v5));
            g.AddEdge(new Edge(v5, v6));
            g.AddEdge(new Edge(v6, v7));

            //GraphAlgorithms.MinimumDominatingSet(g);

            g.AddEdge(new Edge(v7, v1));
            GraphAlgorithms.MinimumDominatingSet(g);
        }
Esempio n. 14
0
        private static void TestDijkstra()
        {
            //Graph for figure 4.15 part A
            Graph g = new Graph();
            Vertex u1 = new Vertex();
            Vertex u2 = new Vertex();
            Vertex u3 = new Vertex();
            Vertex u4 = new Vertex();
            Vertex u5 = new Vertex();
            Vertex u6 = new Vertex();

            g.AddEdge(new Edge(u1, u2, 2));
            g.AddEdge(new Edge(u1, u3, 8));
            g.AddEdge(new Edge(u2, u3, 5));
            g.AddEdge(new Edge(u2, u4, 3));
            g.AddEdge(new Edge(u3, u2, 6));
            g.AddEdge(new Edge(u3, u5, 0));
            g.AddEdge(new Edge(u4, u3, 1));
            g.AddEdge(new Edge(u4, u5, 7));
            g.AddEdge(new Edge(u4, u6, 6));
            g.AddEdge(new Edge(u5, u4, 4));
            g.AddEdge(new Edge(u6, u5, 2));

            Console.WriteLine("Dijkstra Output:");
            Graph tree = GraphAlgorithms.Dijkstra(g, 0, true);
        }
Esempio n. 15
0
 public bool RemoveVertex(Vertex v)
 {
     checkNullGraph(currentGraph);
     currentGraph.RemoveVertex(v);
     return true;
 }
Esempio n. 16
0
        public static void MinimumDominatingSet(Graph g, int seed)
        {
            Output.WriteLine("[Minimum Dominating Set Output]");

            //MDS takes an undirected graph
            CombinationGenerator cg = new CombinationGenerator(g.GetVertices().Count, seed);
            bool setFound = false;
            List<Vertex> verts = new List<Vertex>(g.GetVertices().Count);
            List<Vertex> neighborhood = new List<Vertex>();
            List<Vertex[]> domSet = new List<Vertex[]>();
            bool restricted = false;
            int setLen = 0;

            while (cg.HasNext())
            {
                int[] array = cg.GetNext();
                int[] tally = new int[array.Length];
                array.CopyTo(tally, 0);

                Vertex[] possibleSet = new Vertex[cg.CurrentLevel()];
                int possibleSetCounter = 0;
                List<Vertex> neighbors = new List<Vertex>();

                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] != 1)
                    {
                        continue;
                    }

                    possibleSet[possibleSetCounter] = g.GetVertices()[i];
                    possibleSetCounter++;

                    neighbors.AddRange(g.GetVertices()[i].GetAllNeighbors());
                }

                foreach (Vertex v in neighbors)
                {
                    int index = g.GetVertices().IndexOf(v);
                    if (index == -1)
                    {
                        continue;
                    }

                    tally[index] = 1;
                }

                bool validSet = true;
                for (int i = 0; i < tally.Length; i++)
                {
                    if (tally[i] != 1)
                    {
                        validSet = false;
                        break;  //we break, because we only want to find one dominating set
                    }
                }

                if (validSet)
                {
                    setFound = true;
                    if (!restricted)
                    {
                        cg.RestrictToCurrentLevel();
                        restricted = true;
                        setLen = possibleSet.Length;
                    }

                    if (setLen == possibleSet.Length)
                    {
                        domSet.Add(possibleSet);
                        break;
                    }
                }
            }

            //Tally method
            //Initiate tally to be the generated array, (create a copy!)
            //go through the neighborhood of each vertex with value 1
            //for each neighbor, change their value in the tally to 1
            //If tally is all 1s, then you have everything

            if (setFound)
            {
                //print out the found set and find the others
                Output.WriteLine("Found minimum dominating sets:");
                foreach (Vertex[] v in domSet)
                {
                    string separator = "";
                    for (int i = 0; i < v.Length; i++)
                    {
                        Output.Write(separator + v[i].ToString());
                        separator = ",";
                    }
                    Output.WriteLine();
                }
            }

            Output.WriteLine("[End Minimum Dominating Set Output]");
        }
Esempio n. 17
0
        private static void TestMatrixStuff()
        {
            Graph g = new Graph();
            g.Directed = true;
            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();

            g.AddVertex(v1);
            g.AddVertex(v2);
            g.AddVertex(v3);
            g.AddVertex(v4);

            g.AddEdge(new Edge(v2, v1, 25f));
            g.AddEdge(new Edge(v1, v3, 10f));

            g.RemoveEdge(g.GetEdges()[1]);

            g.AddEdge(new Edge(v4, v2, 7f));

            g.RemoveVertex(v1);
            g.RemoveVertex(v2);

            g.AddEdge(new Edge(v3, v4, 30f));
            g.AddEdge(new Edge(v4, v3, 27f));

            Console.WriteLine("Testing graph insertion and deletion");

            g.PrintMatrix();

            Console.WriteLine();
            Graph g2 = new Graph();
            g.CopyTo(g2);
            g2.PrintMatrix();
        }
Esempio n. 18
0
        private static void TestMaxFlow()
        {
            FlowNetwork fn = new FlowNetwork();

            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();

            fn.AddVertex(v1);
            fn.AddVertex(v2);
            fn.AddVertex(v3);
            fn.AddVertex(v4);
            fn.AddVertex(v5);
            fn.AddVertex(v6);

            fn.SetSource(v1);
            fn.SetSink(v6);

            fn.AddEdge(new FlowEdge(v1, v2, 0f, 2f));
            fn.AddEdge(new FlowEdge(v1, v3, 0f, 2f));
            fn.AddEdge(new FlowEdge(v2, v4, 0f, 2f));
            fn.AddEdge(new FlowEdge(v3, v4, 1f, 2f));
            fn.AddEdge(new FlowEdge(v3, v5, 0f, 2f));
            fn.AddEdge(new FlowEdge(v4, v6, 1f, 2f));
            fn.AddEdge(new FlowEdge(v5, v6, 0f, 2f));

            //float maxFlow = GraphAlgorithms.MaximumFlow(fn);

            //Console.WriteLine("Max flow is " + maxFlow);
        }
Esempio n. 19
0
        public virtual void RemoveVertex(Vertex v)
        {
            int index = vertices.IndexOf(v);
            bool success = vertices.Remove(v);
            if (success)
            {
                vertexCount--;
                v.SetGraph(null);
                //Delete all edges going to and from this vertex
                foreach (Edge e in v.GetInEdges())
                {
                    RemoveEdge(e);
                }
                foreach (Edge e in v.GetOutEdges())
                {
                    RemoveEdge(e);
                }

                matrix.RemoveVertex(index);
            }
        }
Esempio n. 20
0
        private static void TestResidualNetwork()
        {
            //Create graph from Figure 6.10
            FlowNetwork fn = new FlowNetwork();
            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();

            fn.AddVertex(v1);
            fn.AddVertex(v2);
            fn.AddVertex(v3);
            fn.AddVertex(v4);

            fn.SetSource(v1);
            fn.SetSink(v4);

            fn.AddEdge(new FlowEdge(v1, v3, 3, 4));
            fn.AddEdge(new FlowEdge(v1, v2, 2, 2));
            fn.AddEdge(new FlowEdge(v2, v3, 2, 3));
            fn.AddEdge(new FlowEdge(v3, v4, 5, 5));
            fn.AddEdge(new FlowEdge(v2, v4, 0, 1));

            Graph g = GraphAlgorithms.CreateResidualNetwork(fn);
        }
Esempio n. 21
0
        private void copyLinkedGraph(FlowNetwork to)
        {
            to.Clear();

            for (int i = 0; i < this.GetVertices().Count; i++)
            {
                vertices[i].Tag = i;
                Vertex v = new Vertex();
                v.Tag = i;
                to.AddVertex(v);
            }

            for (int i = 0; i < this.GetEdges().Count; i++)
            {
                Edge currentEdge = this.GetEdges()[i];

                Vertex v1 = new Vertex();
                v1.Label = currentEdge.GetFromVertex().Label;
                int cd = (int)currentEdge.GetFromVertex().Tag;
                if ((int)to.GetVertices()[cd].Tag == cd)
                {
                    v1 = to.GetVertices()[cd];
                }

                Vertex v2 = new Vertex();
                v2.Label = currentEdge.GetToVertex().Label;
                int cd2 = (int)currentEdge.GetToVertex().Tag;
                if ((int)to.GetVertices()[cd2].Tag == cd2)
                {
                    v2 = to.GetVertices()[cd2];
                }

                FlowEdge fe = currentEdge as FlowEdge;
                Edge e = new FlowEdge(v1, v2, fe.CurrentFlow, fe.Capacity);

                to.AddEdge(e);
            }

            FlowNetwork fn = this as FlowNetwork;
            if (fn != null)
            {
                to.SetSource(fn.GetSource());
                to.SetSink(fn.GetSink());
            }
        }
Esempio n. 22
0
 public void SetSink(Vertex s)
 {
     sink = s;
 }