Ejemplo n.º 1
0
        /// <summary>
        /// Transposes the actual graph. The direction of all edges is inverted.
        /// </summary>
        public void Transpose()
        {
            foreach (DirectedGraphEdge edge in this.EdgesList)
            {
                string key         = edge.From + "," + edge.To;
                string keyReverse  = edge.To + "," + edge.From;
                bool   occursTwice = this._edges.ContainsKey(keyReverse);
                if (occursTwice && key.CompareTo(keyReverse) < 0)
                {
                    double tempCost = this._edges[keyReverse].Cost;
                    this._edges[keyReverse].Cost = this._edges[key].Cost;
                    this._edges[key].Cost        = tempCost;
                }
                else if (!occursTwice)
                {
                    this._edges.Remove(edge.From + "," + edge.To);
                    DirectedGraphEdge tempEdge = (DirectedGraphEdge)edge.Clone();
                    tempEdge.Transpose();
                    this._edges.Add(edge.To + "," + edge.From, tempEdge);
                }

                //Console.WriteLine("Reverted edge (" + edge.From + ","
                //    + edge.To + ") to edge (" + key + ").");
            }
        }
Ejemplo n.º 2
0
        public bool AdditionalEdgeInducesDirectedCycle(DirectedGraphEdge additionalEdge)
        {
            DirectedGraph tempGraph = (DirectedGraph)this.Clone();

            tempGraph.AddEdge(additionalEdge);
            tempGraph.ComputeStronglyConnectedComponents(null);
            List <GraphNode> query = (from node1 in tempGraph.Nodes
                                      from node2 in tempGraph.Nodes
                                      where ((!node1.Name.Equals(node2.Name)) &&
                                             node1.ComponentNumber == node2.ComponentNumber)
                                      select node1).ToList();

            return(query != null && query.Count() > 0);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds an edge to the graph. If an edge (u,v) already exists,
        /// nothing is done. Use UpdateEdgeWeights to change the weights of existing
        /// edges. The return value of this function signalizes, whether the edge was added
        /// or not.
        /// </summary>
        /// <param name="edge"></param>
        /// <returns>true if edge was added, false otherwise</returns>
        public bool AddEdge(DirectedGraphEdge edge)
        {
            string tempKey = edge.From + "," + edge.To;

            if (!this._edges.ContainsKey(tempKey))
            {
                this._edges.Add(tempKey, edge);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 4
0
        public bool EdgeReversalInducesDirectedCycle(DirectedGraphEdge edge)
        {
            DirectedGraph tempGraph = (DirectedGraph)this.Clone();

            tempGraph.RemoveEdge(edge.From, edge.To);
            tempGraph.AddEdge(new DirectedGraphEdge(edge.To, edge.From, edge.Cost));
            tempGraph.ComputeStronglyConnectedComponents(null);
            List <GraphNode> query = (from node1 in tempGraph.Nodes
                                      from node2 in tempGraph.Nodes
                                      where ((!node1.Name.Equals(node2.Name)) &&
                                             node1.ComponentNumber == node2.ComponentNumber)
                                      select node1).ToList();

            return(query != null && query.Count() > 0);
        }
Ejemplo n.º 5
0
 public void UpdateEdgeName(string key, string newFrom, string newTo)
 {
     if (this._edges.ContainsKey(key))
     {
         DirectedGraphEdge tempEdge = new DirectedGraphEdge(newFrom, newTo, 0);
         if (!this._edges.ContainsKey(tempEdge.Key))
         {
             tempEdge = this._edges[key];
             this._edges.Remove(key);
             tempEdge.ChangeEdgeFrom(newFrom);
             tempEdge.ChangeEdgeTo(newTo);
             this._edges.Add(tempEdge.Key, tempEdge);
         }
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Helper function, which is used by ComputeCompelledAndReversibleEdges()
 /// </summary>
 /// <param name="edge"></param>
 /// <param name="reversibleEdges"></param>
 private void _addReversibleEdge(DirectedGraphEdge edge,
                                 ref Dictionary <string, List <DirectedGraphEdge> > reversibleEdges)
 {
     if (edge.Label == GraphEdgeLabel.UNLABELLED)
     {
         edge.Label = GraphEdgeLabel.REVERSIBLE;
         if (reversibleEdges.ContainsKey(edge.To))
         {
             reversibleEdges[edge.To].Add(edge);
         }
         else
         {
             List <DirectedGraphEdge> tempReversibleEdges = new List <DirectedGraphEdge>();
             tempReversibleEdges.Add(edge);
             reversibleEdges.Add(edge.To, tempReversibleEdges);
         }
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Helper function, which is used by ComputeCompelledAndReversibleEdges()
 /// </summary>
 /// <param name="edge"></param>
 /// <param name="compelledEdges"></param>
 private void _addCompelledEdge(DirectedGraphEdge edge,
                                ref Dictionary <string, List <DirectedGraphEdge> > compelledEdges)
 {
     if (edge.Label == GraphEdgeLabel.UNLABELLED)
     {
         edge.Label = GraphEdgeLabel.COMPLELLED;
         if (compelledEdges.ContainsKey(edge.To))
         {
             compelledEdges[edge.To].Add(edge);
         }
         else
         {
             List <DirectedGraphEdge> tempCompelledEdges = new List <DirectedGraphEdge>();
             tempCompelledEdges.Add(edge);
             compelledEdges.Add(edge.To, tempCompelledEdges);
         }
     }
 }
Ejemplo n.º 8
0
        public void ComputeCompelledAndReversibleEdges()
        {
            List <DirectedGraphEdge> orderedEdges = this._getOrderedEdges();
            Dictionary <string, List <DirectedGraphEdge> > reversibleEdges = new Dictionary <string, List <DirectedGraphEdge> >();
            Dictionary <string, List <DirectedGraphEdge> > compelledEdges  = new Dictionary <string, List <DirectedGraphEdge> >();
            Dictionary <string, List <DirectedGraphEdge> > unlabelledEdges = new Dictionary <string, List <DirectedGraphEdge> >();
            bool doneForThisCycle = false;

            foreach (var edge in orderedEdges)
            {
                edge.Label = GraphEdgeLabel.UNLABELLED;
                if (unlabelledEdges.ContainsKey(edge.To))
                {
                    unlabelledEdges[edge.To].Add(edge);
                }
                else
                {
                    List <DirectedGraphEdge> tempEdges = new List <DirectedGraphEdge>();
                    tempEdges.Add(edge);
                    unlabelledEdges.Add(edge.To, tempEdges);
                }
            }

            foreach (DirectedGraphEdge edge in orderedEdges)
            {
                if (edge.Label == GraphEdgeLabel.UNLABELLED)
                {
                    doneForThisCycle = false;

                    if (compelledEdges.ContainsKey(edge.From))
                    {
                        List <DirectedGraphEdge> tempCompelledEdges = compelledEdges[edge.From];
                        foreach (DirectedGraphEdge compelledEdge in tempCompelledEdges)
                        {
                            string tempKey = compelledEdge.From + "," + edge.To;
                            if (!this._edges.ContainsKey(tempKey))
                            {
                                if (edge.Label != GraphEdgeLabel.COMPLELLED)
                                {
                                    this._addCompelledEdge(edge, ref compelledEdges);
                                    unlabelledEdges[edge.To].Remove(edge);
                                }

                                if (unlabelledEdges.ContainsKey(edge.To))
                                {
                                    foreach (DirectedGraphEdge tempEdge in unlabelledEdges[edge.To])
                                    {
                                        this._addCompelledEdge(tempEdge, ref compelledEdges);
                                    }
                                    unlabelledEdges.Remove(edge.To);
                                }
                                doneForThisCycle = true;
                            }
                            else
                            {
                                DirectedGraphEdge tempEdge = this._edges[tempKey];
                                if (tempEdge.Label == GraphEdgeLabel.UNLABELLED)
                                {
                                    this._addCompelledEdge(tempEdge, ref compelledEdges);
                                    unlabelledEdges[tempEdge.To].Remove(tempEdge);
                                }
                            }
                        }
                    }
                    if (!doneForThisCycle)
                    {
                        IEnumerable <DirectedGraphEdge> query =
                            from queryEdge in Edges
                            where queryEdge.To == edge.To && queryEdge.From != edge.From
                            select queryEdge;
                        bool containsSpecialEdge = false;
                        foreach (DirectedGraphEdge testEdge in query)
                        {
                            string tempKey = testEdge.From + "," + edge.From;
                            if (!this._edges.ContainsKey(tempKey))
                            {
                                containsSpecialEdge = true;
                                break;
                            }
                        }
                        if (containsSpecialEdge)
                        {
                            this._addCompelledEdge(edge, ref compelledEdges);
                            unlabelledEdges[edge.To].Remove(edge);
                            if (unlabelledEdges.ContainsKey(edge.To))
                            {
                                foreach (DirectedGraphEdge tempEdge in unlabelledEdges[edge.To])
                                {
                                    this._addCompelledEdge(tempEdge, ref compelledEdges);
                                }
                                unlabelledEdges.Remove(edge.To);
                            }
                        }
                        else
                        {
                            this._addReversibleEdge(edge, ref reversibleEdges);
                            unlabelledEdges[edge.To].Remove(edge);
                            if (unlabelledEdges.ContainsKey(edge.To))
                            {
                                foreach (DirectedGraphEdge tempEdge in unlabelledEdges[edge.To])
                                {
                                    this._addReversibleEdge(tempEdge, ref reversibleEdges);
                                }
                                unlabelledEdges.Remove(edge.To);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public List <string> SampleOrder()
        {
            DirectedGraph tempGraph    = (DirectedGraph)this._restrictionGraph.Clone();
            List <string> orderedNodes = new List <string>();

            int[][] tempRestrictionMatrix = (int[][])this._standardRestrictionMatrix.Clone();
            double  sum          = 0;
            double  randomNumber = 0;
            double  tempSum      = 0;
            int     index        = 0;

            List <Tuple <double, Tuple <uint, uint> > > choices = _getPossibleChoices(tempRestrictionMatrix, ref sum);

            while (choices.Count > 0)
            {
                index = 0;

                // if all allowed choices in M are zero, we draw the index from a uniform distribution
                // otherwise, the entry M[i,j] is proportional to its probability to be chosen
                if (sum - 0.0001 < 0 && sum + 0.0001 > 0)
                {
                    index = _randomNumbers.Next(choices.Count);
                }
                else
                {
                    randomNumber = ((double)this._randomNumbers.Next()) / Int32.MaxValue * sum;

                    //find choice that corresponds to random number

                    tempSum = 0;
                    do
                    {
                        tempSum += choices[index++].Item1;
                    }while (index < choices.Count && tempSum < randomNumber - 0.0001);
                    // this should not happen
                    if (index == choices.Count)
                    {
                        --index;
                    }
                }
                //add edge to restriction graph
                DirectedGraphEdge chosenEdge = new DirectedGraphEdge(this._indexToName[choices[index].Item2.Item1],
                                                                     this._indexToName[choices[index].Item2.Item2],
                                                                     0);
                tempGraph.AddEdge(chosenEdge);
                //update restrictions
                HashSet <string> newChildren = tempGraph.GetReachableNodes(chosenEdge.To);
                newChildren.Add(chosenEdge.To);
                tempRestrictionMatrix[this._nameToIndex[chosenEdge.From]][this._nameToIndex[chosenEdge.To]] = 1;
                HashSet <string> ancestors = tempGraph.GetAncestors(chosenEdge.From);
                foreach (string newChild in newChildren)
                {
                    tempRestrictionMatrix[this._nameToIndex[newChild]][this._nameToIndex[chosenEdge.From]] = 1;
                    foreach (string ancestor in ancestors)
                    {
                        tempRestrictionMatrix[this._nameToIndex[newChild]][this._nameToIndex[ancestor]] = 1;
                    }
                }
                // prepare next round
                sum     = 0;
                choices = _getPossibleChoices(tempRestrictionMatrix, ref sum);
            }
            // if there are no free choices, tempGraph contains all choices and has therefore a unique topological order
            return(tempGraph.GetTopologicalOrder());
        }