Ejemplo n.º 1
0
        /// <summary>
        /// Saves a graph to a file in the GraphML format.
        /// </summary>
        /// <typeparam name="TNode"></typeparam>
        /// <typeparam name="TLabel"></typeparam>
        /// <param name="graph"></param>
        /// <param name="labels"></param>
        public static void SaveToGraphML <TNode, TLabel>(MultiDirectedGraph <TNode, TLabel> graph, string path)
        {
            List <string> lines = new List <string>();

            lines.Add("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            lines.Add("<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">");

            lines.Add("\t<key id=\"label\" for=\"all\" attr.name=\"label\" attr.type=\"string\">");
            lines.Add("\t\t<default>0</default>");
            lines.Add("\t</key>");

            lines.Add("\t<graph id=\"" + graph.Name + "\" edgedefault=\"" + (graph.IsDirected ? "directed" : "undirected") + "\">");

            foreach (var node in graph.Nodes)
            {
                lines.Add("\t\t<node id=\"" + node + "\">");
                lines.Add("\t\t\t<data key=\"label\">" + graph.NodeLabel(node) + "</data>");
                lines.Add("\t\t</node>");
            }

            foreach (var edge in graph.Edges)
            {
                var s = graph.Source(edge);
                var t = graph.Target(edge);
                lines.Add("\t\t<edge source=\"" + s + "\" target=\"" + t + "\">");
                lines.Add("\t\t\t<data key=\"label\">" + graph.EdgeLabel(edge) + "</data>");
                lines.Add("\t\t</edge>");
            }

            lines.Add("\t</graph>");
            lines.Add("</graphml>");

            File.WriteAllLines(path, lines);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="owner"></param>
        private void setOwner(IDictionary <TNode, ExactBisimulationWorker <TNode, TLabel> > owner)
        {
            this.owner         = owner;
            this.ownerInverted = Utils.Invert(owner);
            this.interestedIn  = new Dictionary <ExactBisimulationWorker <TNode, TLabel>, HashSet <TNode> >();

            // Initialize interested-in function
            foreach (var worker in ownerInverted.Keys)
            {
                interestedIn.Add(worker, new HashSet <TNode>());
            }
            foreach (var target in ownerInverted[this])
            {
                foreach (var ei in graph.In(target))
                {
                    var source = graph.Source(ei);

                    if (!interestedIn[owner[source]].Contains(target))
                    {
                        interestedIn[owner[source]].Add(target);
                    }
                }
            }
            interestedIn.Remove(this);
        }
        /// <summary>
        /// Measures the cut of a partition of a graph.
        /// </summary>
        /// <typeparam name="TNode"></typeparam>
        /// <typeparam name="TLabel"></typeparam>
        /// <param name="graph"></param>
        /// <param name="partition"></param>
        /// <returns></returns>
        public static int Cut <TNode, TLabel>(MultiDirectedGraph <TNode, TLabel> graph, IDictionary <TNode, int> partition)
        {
            int cut = 0;

            foreach (var edge in graph.Edges)
            {
                var s = graph.Source(edge);
                var t = graph.Target(edge);

                if (partition[s] != partition[t])
                {
                    cut += 1;
                }
            }

            return(cut);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sample edges randomly uniformly.
        /// </summary>
        /// <typeparam name="TNode"></typeparam>
        /// <param name="graph"></param>
        /// <param name="m">Upper bound on the number of edges to sample.</param>
        /// <returns></returns>
        public static IEnumerable <TNode> RE <TNode, TLabel>(this MultiDirectedGraph <TNode, TLabel> graph, int m)
        {
            var edges = Utils.Shuffled(graph.Edges.ToArray()).Take(Math.Min(m, graph.NumEdges)).ToArray();
            var nodes = new HashSet <TNode>();

            foreach (var edge in edges)
            {
                var s = graph.Source(edge);
                var t = graph.Target(edge);

                if (!nodes.Contains(s))
                {
                    nodes.Add(s);
                }

                if (!nodes.Contains(t))
                {
                    nodes.Add(t);
                }
            }

            return(nodes);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// KerninghanLin algorithm which refines a partition.
        /// Equalizes block sizes and swaps nodes between partition blocks to minimize the edge cut.
        /// </summary>
        /// <typeparam name="TNode">Type of node.</typeparam>
        /// <typeparam name="TLabel">Type of label.</typeparam>
        /// <param name="graph">The graph of the partition.</param>
        /// <param name="partition">The partition to refine.</param>
        /// <param name="K">Maximum number of iterations.</param>
        public static void KernighanLin <TNode, TLabel>(MultiDirectedGraph <TNode, TLabel> graph, Dictionary <TNode, int> partition, int K)
        {
            // Compute ED and ID of each node
            var ED = new Dictionary <TNode, int>();
            var ID = new Dictionary <TNode, int>();
            Func <TNode, int> D = node => ED[node] - ID[node];

            foreach (var node in graph.Nodes)
            {
                ED.Add(node, 0);
                ID.Add(node, 0);
            }

            foreach (var edge in graph.Edges)
            {
                var s = graph.Source(edge);
                var t = graph.Target(edge);

                if (partition[s] == partition[t])
                {
                    ID[s] += 1;
                    ID[t] += 1;
                }
                else
                {
                    ED[s] += 1;
                    ED[t] += 1;
                }
            }

            // Stick nodes into sets of their partition block
            var inverted = Utils.Invert(partition);
            var AI       = inverted.Keys.First();
            var BI       = inverted.Keys.Last();

            // Swaps a node from its original partition block to the other
            Action <TNode> swap = node =>
            {
                foreach (var edge in graph.Out(node).Concat(graph.In(node)))
                {
                    var neighbor = graph.Target(edge);

                    if (node.Equals(neighbor))
                    {
                        continue;
                    }

                    if (partition[node] == partition[neighbor])
                    {
                        // Will be in other block now
                        ID[neighbor] -= 1;
                        ID[node]     -= 1;
                        ED[neighbor] += 1;
                        ED[node]     += 1;
                    }
                    else
                    {
                        // Will be in same block now
                        ID[neighbor] += 1;
                        ID[node]     += 1;
                        ED[neighbor] -= 1;
                        ED[node]     -= 1;
                    }
                }

                if (partition[node] == AI)
                {
                    partition[node] = BI;
                    inverted[AI].Remove(node);
                    inverted[BI].Add(node);
                }
                else
                {
                    partition[node] = AI;
                    inverted[AI].Add(node);
                    inverted[BI].Remove(node);
                }
            };

            // Equalize block sizes
            while (Math.Abs(inverted[AI].Count - inverted[BI].Count) > 1)
            {
                if (inverted[AI].Count > inverted[BI].Count)
                {
                    // Move from A to B
                    var a = inverted[AI].MaxBy(node => D(node));
                    swap(a);
                }
                else
                {
                    // Move from B to A
                    var b = inverted[BI].MaxBy(node => D(node));
                    swap(b);
                }
            }

            // Keep performing positive swaps
            int n = Math.Min(inverted[AI].Count, inverted[BI].Count);

            for (int i = 0; i < K; i++)
            {
                bool hasGained = false;
                var  AA        = new HashSet <TNode>(inverted[AI]);
                var  BB        = new HashSet <TNode>(inverted[BI]);

                for (int j = 0; j < n; j++)
                {
                    var a    = AA.MaxBy(node => D(node));
                    var b    = BB.MaxBy(node => D(node));
                    int gain = D(a) + D(b);

                    if (graph.HasEdge(a, b))
                    {
                        gain -= 2;
                    }

                    if (graph.HasEdge(b, a))
                    {
                        gain -= 2;
                    }

                    if (gain > 0)
                    {
                        hasGained = true;
                        swap(a);
                        swap(b);
                    }

                    AA.Remove(a);
                    BB.Remove(b);
                }

                if (!hasGained)
                {
                    break;
                }
            }
        }
        /// <summary>
        /// Measures the weighted k-bisimulation partition equivalence between two graphs.
        /// </summary>
        /// <typeparam name="TNode"></typeparam>
        /// <typeparam name="TLabel"></typeparam>
        /// <param name="G1"></param>
        /// <param name="G2"></param>
        /// <param name="L1"></param>
        /// <param name="L2"></param>
        /// <param name="k">A tuple indicating how many nodes of G1 and G2 are in partition blocks that are shared between G1 and G2.</param>
        /// <returns></returns>
        public static Tuple <int, int> WeightedBisimulationEquivalence <TNode, TLabel>(MultiDirectedGraph <TNode, TLabel> G1, MultiDirectedGraph <TNode, TLabel> G2, int k)
        {
            // Create new empty graph and label provider
            var G = new MultiDirectedGraph <Tuple <int, TNode>, TLabel>();

            // Add nodes of G1
            foreach (var node in G1.Nodes.Select(node => Tuple.Create(1, node)))
            {
                G.AddNode(node, G1.NodeLabel(node.Item2));
            }

            // Add nodes of G2
            foreach (var node in G2.Nodes.Select(node => Tuple.Create(2, node)))
            {
                G.AddNode(node, G2.NodeLabel(node.Item2));
            }

            // Add edges of G1
            foreach (var edge in G1.Edges)
            {
                var s = Tuple.Create(1, G1.Source(edge));
                var t = Tuple.Create(1, G1.Target(edge));
                G.AddEdge(s, t, G1.EdgeLabel(edge));
            }

            // Add edges of G2
            foreach (var edge in G2.Edges)
            {
                var s = Tuple.Create(2, G2.Source(edge));
                var t = Tuple.Create(2, G2.Target(edge));
                G.AddEdge(s, t, G2.EdgeLabel(edge));
            }

            // Perform bisimulation reduction
            var partitioner = new GraphPartitioner <Tuple <int, TNode>, TLabel>(G);
            var partition   = partitioner.BoundedExactBisimulationReduction(k);

            // Partition blocks of G1 and G2
            HashSet <int> P1 = new HashSet <int>();
            HashSet <int> P2 = new HashSet <int>();

            foreach (var node in G.Nodes)
            {
                int block = partition[node];

                switch (node.Item1)
                {
                case 1:
                    if (!P1.Contains(block))
                    {
                        P1.Add(block);
                    }
                    break;

                case 2:
                    if (!P2.Contains(block))
                    {
                        P2.Add(block);
                    }
                    break;
                }
            }

            int s1 = 0;
            int s2 = 0;

            foreach (var node in G.Nodes)
            {
                if (P1.Contains(partition[node]) && P2.Contains(partition[node]))
                {
                    switch (node.Item1)
                    {
                    case 1:
                        s1 += 1;
                        break;

                    case 2:
                        s2 += 1;
                        break;
                    }
                }
            }

            return(Tuple.Create(s1, s2));
        }
Ejemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="D"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static MultiDirectedGraph <int, int> GenerateNiceDAG(int D, int b)
        {
            // Create empty graph and label provider
            var graph = new MultiDirectedGraph <int, int>();

            graph.Name = "Synthetic_DAG_" + D + "_" + b;

            // Define parent function
            Func <int, int> parent = node =>
            {
                // Assume node > 0 (not the root node)
                return(graph.In(node).First());
            };

            // Define level function
            Func <int, int> level = Utils.Y <int, int>(fix => node =>
            {
                if (node == 0)
                {
                    // Root node
                    return(0);
                }
                else
                {
                    return(1 + fix(parent(node)));
                }
            });

            // Create initial tree
            int counter = 0;

            graph.AddNode(counter, 0);
            counter += 1;

            while (graph.Nodes.Select(node => level(node)).Max() < D)
            {
                int max    = graph.Nodes.Select(node => level(node)).Max();
                var lowest = graph.Nodes.Where(node => level(node) == max).ToArray();

                foreach (var node in lowest)
                {
                    int k = StaticRandom.Next(b + 1);

                    for (int i = 0; i < k; i++)
                    {
                        graph.AddNode(counter, 0);
                        graph.AddEdge(node, counter, i);
                        // graph.AddEdge(node, counter, 0);
                        counter += 1;
                    }
                }
            }

            // Transform tree to DAG with nicer partition block distribution
            var copy              = graph.Clone();
            var partitioner       = new GraphPartitioner <int, int>(graph);
            var partition         = partitioner.BoundedExactBisimulationReduction(D);
            var partitionInverted = Utils.Invert(partition);
            var blocks            = partition.Values.Distinct();
            var blockSizes        = Utils.Distribution(partition.Values);
            int blockMax          = blockSizes.Values.Max();

            foreach (var block in blocks)
            {
                int size  = blockSizes[block];
                var nodes = new List <int>(partitionInverted[block]);

                for (int i = size; i < blockMax; i++)
                {
                    // Replicate a random node in this partition block
                    int k = StaticRandom.Next(size);
                    var v = nodes[k];

                    // Replicate the node
                    graph.AddNode(counter, graph.NodeLabel(v));

                    // Replicate its incoming edges
                    foreach (var ei in copy.In(v))
                    {
                        var u = graph.Source(ei);
                        graph.AddEdge(u, counter, graph.EdgeLabel(ei));
                    }

                    // Replicate its outgoing edges
                    foreach (var eo in copy.Out(v))
                    {
                        var w = graph.Target(eo);
                        graph.AddEdge(counter, w, graph.EdgeLabel(eo));
                    }

                    counter += 1;
                }
            }

            return(graph);
        }