Пример #1
0
        internal void EdgeOnPathFinderTest()
        {
            var g = new Graph <BasicEdgeNode, BasicEdge>(BasicEdge.New);
            var u = new BasicEdgeNode("u");
            var v = new BasicEdgeNode("v");
            var w = new BasicEdgeNode("w");
            var x = new BasicEdgeNode("x");
            var y = new BasicEdgeNode("y");
            var z = new BasicEdgeNode("z");

            g.Nodes.Add(u);
            g.Nodes.Add(v);
            g.Nodes.Add(w);
            g.Nodes.Add(x);
            g.Nodes.Add(y);
            g.Nodes.Add(z);
            g.AddEdge(u, v);
            g.AddEdge(v, w);
            g.AddEdge(w, x);
            g.AddEdge(x, v);
            g.AddEdge(v, y);
            g.AddEdge(v, z);
            g.AddEdge(z, u);

            var edgesOnPath = new HashSet <BasicEdge>();
            var pf          = new EdgeOnPathFinder <BasicEdgeNode, BasicEdge>(g.EdgesOutOf, g.TargetOf, g, MakeIndexedProperty.FromSet(edgesOnPath), node => node.Data.Equals("z"));

            pf.SearchFrom(u);
            Console.Write("edges on path: ");
            Console.WriteLine(StringUtil.CollectionToString(edgesOnPath, " "));
        }
Пример #2
0
        internal static Graph <BasicEdgeNode, BasicEdge> WestGraph2()
        {
            // this is the example graph in West sec 7.3.21
            var           g = new Graph <BasicEdgeNode, BasicEdge>(BasicEdge.New);
            BasicEdgeNode u = new BasicEdgeNode("u");
            BasicEdgeNode v = new BasicEdgeNode("v");
            BasicEdgeNode w = new BasicEdgeNode("w");
            BasicEdgeNode x = new BasicEdgeNode("x");
            BasicEdgeNode y = new BasicEdgeNode("y");
            BasicEdgeNode z = new BasicEdgeNode("z");

            g.Nodes.Add(u);
            g.Nodes.Add(v);
            g.Nodes.Add(w);
            g.Nodes.Add(x);
            g.Nodes.Add(y);
            g.Nodes.Add(z);
            g.AddEdge(u, v);
            g.AddEdge(v, w);
            g.AddEdge(w, x);
            g.AddEdge(w, y);
            g.AddEdge(x, u);
            g.AddEdge(x, y);
            g.AddEdge(y, z);
            g.AddEdge(z, u);
            g.AddEdge(z, v);
            return(g);
        }
Пример #3
0
        public void MinCutTest()
        {
            Graph <BasicEdgeNode, BasicEdge> g = new Graph <BasicEdgeNode, BasicEdge>(BasicEdge.New);
            // this graph is from Cormen et al, fig 27.1
            BasicEdgeNode s  = new BasicEdgeNode("s");
            BasicEdgeNode v1 = new BasicEdgeNode("v1");
            BasicEdgeNode v2 = new BasicEdgeNode("v2");
            BasicEdgeNode v3 = new BasicEdgeNode("v3");
            BasicEdgeNode v4 = new BasicEdgeNode("v4");
            BasicEdgeNode t  = new BasicEdgeNode("t");

            g.Nodes.Add(s);
            g.Nodes.Add(v1);
            g.Nodes.Add(v2);
            g.Nodes.Add(v3);
            g.Nodes.Add(v4);
            g.Nodes.Add(t);
            Dictionary <BasicEdge, float> capacity = new Dictionary <BasicEdge, float>();
            BasicEdge e;

            e           = g.AddEdge(s, v1);
            capacity[e] = 16;
            e           = g.AddEdge(s, v2);
            capacity[e] = 13;
            e           = g.AddEdge(v1, v2);
            capacity[e] = 10;
            e           = g.AddEdge(v1, v3);
            capacity[e] = 12;
            e           = g.AddEdge(v2, v1);
            capacity[e] = 4;
            e           = g.AddEdge(v2, v4);
            capacity[e] = 14;
            e           = g.AddEdge(v3, v2);
            capacity[e] = 9;
            e           = g.AddEdge(v3, t);
            capacity[e] = 20;
            e           = g.AddEdge(v4, v3);
            capacity[e] = 7;
            e           = g.AddEdge(v4, t);
            capacity[e] = 4;
            var mc = new MinCut <BasicEdgeNode, BasicEdge>(g, e2 => capacity[e2]);

            mc.Sources.Add(s);
            mc.Sinks.Add(t);
            // sourceGroup should be s,v1,v2,v4
            Set <BasicEdgeNode> sourceGroup = mc.GetSourceGroup();

            Console.WriteLine("sourceGroup = {0}", sourceGroup);
            Assert.True(sourceGroup.Count == 4 && sourceGroup.ContainsAll(new BasicEdgeNode[] { s, v1, v2, v4 }));
            mc.Sources.Add(v1);
            mc.Sinks.Add(v3);
            sourceGroup = mc.GetSourceGroup();
            Console.WriteLine("sourceGroup = {0}", sourceGroup);
            Assert.True(sourceGroup.Count == 4 && sourceGroup.ContainsAll(new BasicEdgeNode[] { s, v1, v2, v4 }));
            capacity[g.GetEdge(v2, v4)] = 1;
            sourceGroup = mc.GetSourceGroup();
            Console.WriteLine("sourceGroup = {0}", sourceGroup);
            Assert.True(sourceGroup.Count == 3 && sourceGroup.ContainsAll(new BasicEdgeNode[] { s, v1, v2 }));
        }
Пример #4
0
        /// <summary>
        /// Test of solving an initializer scheduling problem as a min cut problem.
        /// </summary>
        //[Fact]
        internal void MinCutTest3()
        {
            Graph <BasicEdgeNode, BasicEdge> g = new Graph <BasicEdgeNode, BasicEdge>(BasicEdge.New);
            BasicEdgeNode s  = new BasicEdgeNode("s");
            BasicEdgeNode v1 = new BasicEdgeNode("v1");
            BasicEdgeNode v2 = new BasicEdgeNode("v2");
            BasicEdgeNode v3 = new BasicEdgeNode("v3");
            BasicEdgeNode b1 = new BasicEdgeNode("b1");
            BasicEdgeNode b2 = new BasicEdgeNode("b2");
            BasicEdgeNode t  = new BasicEdgeNode("t");

            g.Nodes.Add(s);
            g.Nodes.Add(v1);
            g.Nodes.Add(v2);
            g.Nodes.Add(v3);
            g.Nodes.Add(b1);
            g.Nodes.Add(b2);
            g.Nodes.Add(t);
            Dictionary <BasicEdge, float> capacity = new Dictionary <BasicEdge, float>();

            foreach (var node in new[] { v1, v2, v3 })
            {
                BasicEdge e2 = g.AddEdge(s, node);
                capacity[e2] = 1;
            }
            BasicEdgeNode[,] pairs =
            {
                { v1, b1 },
                { v2, b1 },
                { v2, b2 },
                { v3, b2 }
            };
            for (int i = 0; i < pairs.GetLength(0); i++)
            {
                var       source = pairs[i, 0];
                var       target = pairs[i, 1];
                BasicEdge e2     = g.AddEdge(source, target);
                capacity[e2] = 1;
            }
            BasicEdge e;

            e           = g.AddEdge(b1, t);
            capacity[e] = 1.1f;
            e           = g.AddEdge(b2, t);
            capacity[e] = 1.1f;
            var mc = new MinCut <BasicEdgeNode, BasicEdge>(g, e2 => capacity[e2]);

            mc.Sources.Add(s);
            mc.Sinks.Add(t);
            Set <BasicEdgeNode> sourceGroup = mc.GetSourceGroup();

            Console.WriteLine("sourceGroup = {0}", sourceGroup);
        }
Пример #5
0
        internal void PathFinderTest2()
        {
            var           g = WestGraph2();
            BasicEdgeNode u = g.Nodes.Where(node => (string)node.Data == "u").First();
            PathFinder <BasicEdgeNode, BasicEdge> pf = new PathFinder <BasicEdgeNode, BasicEdge>(g);

            pf.AddEdge   += delegate(BasicEdge edge) { Console.Write(edge); };
            pf.BeginPath += delegate() { Console.Write("["); };
            pf.EndPath   += delegate() { Console.Write("]"); };
            pf.SearchFrom(u);
            Console.WriteLine();
        }
Пример #6
0
        public void MinCutCycleTest()
        {
            Graph <BasicEdgeNode, BasicEdge> g = new Graph <BasicEdgeNode, BasicEdge>(BasicEdge.New);
            // cycle graph
            BasicEdgeNode s  = new BasicEdgeNode("s");
            BasicEdgeNode v1 = new BasicEdgeNode("v1");
            BasicEdgeNode v2 = new BasicEdgeNode("v2");
            BasicEdgeNode t  = new BasicEdgeNode("t");

            g.Nodes.Add(s);
            g.Nodes.Add(v1);
            g.Nodes.Add(v2);
            g.Nodes.Add(t);
            Dictionary <BasicEdge, float> capacity = new Dictionary <BasicEdge, float>();
            BasicEdge e;

            e           = g.AddEdge(s, v1);
            capacity[e] = 16;
            e           = g.AddEdge(v1, v2);
            capacity[e] = 10;
            e           = g.AddEdge(v2, s);
            capacity[e] = 14;
            var mc = new MinCut <BasicEdgeNode, BasicEdge>(g, e2 => capacity[e2]);

            mc.Sources.Add(s);
            mc.Sinks.Add(s);
            // sourceGroup should be s,v1
            Set <BasicEdgeNode> sourceGroup = mc.GetSourceGroup();

            Console.WriteLine("sourceGroup = {0}", sourceGroup);
            Assert.True(sourceGroup.Count == 2 && sourceGroup.ContainsAll(new BasicEdgeNode[] { s, v1 }));
            mc.Sinks.Remove(s);
            // test the case where t is not reachable from s
            mc.Sinks.Add(t);
            // sourceGroup should be s,v1,v2
            sourceGroup = mc.GetSourceGroup();
            Console.WriteLine("sourceGroup = {0}", sourceGroup);
            Assert.True(sourceGroup.Count == 3 && sourceGroup.ContainsAll(new BasicEdgeNode[] { s, v1, v2 }));
            // test IsSinkEdge
            mc.Sinks.Remove(t);
            e             = g.GetEdge(v2, s);
            mc.IsSinkEdge = edge => edge.Equals(e);
            // sourceGroup should be s,v1
            sourceGroup = mc.GetSourceGroup();
            Console.WriteLine("sourceGroup = {0}", sourceGroup);
            Assert.True(sourceGroup.Count == 2 && sourceGroup.ContainsAll(new BasicEdgeNode[] { s, v1 }));
        }
Пример #7
0
        public void GraphEdgeTest()
        {
            Graph <BasicEdgeNode, BasicEdge> g = new Graph <BasicEdgeNode, BasicEdge>(BasicEdge.New);
            BasicEdgeNode a = new BasicEdgeNode("a");
            BasicEdgeNode b = new BasicEdgeNode("b");
            BasicEdgeNode c = new BasicEdgeNode("c");

            g.Nodes.Add(a);
            g.Nodes.Add(a); // duplicate should be ignored
            g.Nodes.Add(b);
            g.Nodes.Add(c);
            BasicEdge ab  = g.AddEdge(a, b);
            BasicEdge bb  = g.AddEdge(b, b); // self loop
            BasicEdge bc  = g.AddEdge(b, c);
            BasicEdge bc2 = g.AddEdge(b, c); // double edge

            Assert.Equal(c, bc.Target);
            Assert.Equal(c, g.TargetOf(bc));
            Assert.Equal(bb, g.GetEdge(b, b));
            Assert.Equal(0, g.EdgeCount(a, c));
            Assert.Equal(2, g.EdgeCount(b, c));
            Assert.Equal(4, g.EdgeCount());
            Assert.Equal(1, g.NeighborCount(a));
            Assert.Equal(2, g.NeighborCount(c));
            Assert.Equal(1, g.TargetCount(a));
            Assert.Equal(0, g.SourceCount(a));

            Console.Write("EdgesOf(b):");
            foreach (BasicEdge edge in g.EdgesOf(b))
            {
                Console.Write(" {0}", edge);
            }
            Console.WriteLine();
            Console.Write("EdgesInto(b):");
            foreach (BasicEdge edge in g.EdgesInto(b))
            {
                Console.Write(" {0}", edge);
            }
            Console.WriteLine();
            Console.Write("EdgesOutOf(b):");
            foreach (BasicEdge edge in g.EdgesOutOf(b))
            {
                Console.Write(" {0}", edge);
            }
            Console.WriteLine();
            Console.Write("EdgesLinking(b,c):");
            foreach (BasicEdge edge in g.EdgesLinking(b, c))
            {
                Console.Write(" {0}", edge);
            }
            Console.WriteLine();

            // clone the graph
            Graph <BasicEdgeNode, BasicEdge> g2 = new Graph <BasicEdgeNode, BasicEdge>(g);

            g.RemoveEdge(bc2);
            Assert.Equal(1, g.NeighborCount(c));
            g.RemoveEdge(b, c);
            Assert.Equal(0, g.NeighborCount(c));
            g.ClearEdgesOf(a);
            Assert.Equal(0, g.NeighborCount(a));
            g.ClearEdges();
            Assert.Equal(0, g.NeighborCount(b));
            g.Clear();

            Assert.Equal(4, g2.EdgeCount());
        }