Exemple #1
0
 public Node[] Load(string filename, Directedness directedness)
 {
     using (StreamReader reader = new StreamReader(filename))
     {
         return(Load(reader, directedness));
     }
 }
Exemple #2
0
        public Arc AddArc(Node u, Node v, Directedness directedness)
        {
            if (ArcCount(ArcFilter.All) == 2147483647)
            {
                throw new InvalidOperationException("Error: too many arcs!");
            }
            Arc arc = new Arc(arcAllocator.Allocate());

            arcs.Add(arc);
            bool flag = directedness == Directedness.Undirected;

            arcProperties[arc] = new ArcProperties(u, v, flag);
            Utils.MakeEntry(nodeArcs_All, u).Add(arc);
            Utils.MakeEntry(nodeArcs_Forward, u).Add(arc);
            Utils.MakeEntry(nodeArcs_Backward, v).Add(arc);
            if (flag)
            {
                edges.Add(arc);
                Utils.MakeEntry(nodeArcs_Edge, u).Add(arc);
            }
            if (v != u)
            {
                Utils.MakeEntry(nodeArcs_All, v).Add(arc);
                if (flag)
                {
                    Utils.MakeEntry(nodeArcs_Edge, v).Add(arc);
                    Utils.MakeEntry(nodeArcs_Forward, v).Add(arc);
                    Utils.MakeEntry(nodeArcs_Backward, u).Add(arc);
                }
            }
            return(arc);
        }
 public CompleteBipartiteGraph(int redNodeCount, int blueNodeCount, Directedness directedness)
 {
     if (redNodeCount < 0 || blueNodeCount < 0)
     {
         throw new ArgumentException("Invalid node count: " + redNodeCount + ";" + blueNodeCount);
     }
     if ((long)redNodeCount + (long)blueNodeCount > 2147483647 || (long)redNodeCount * (long)blueNodeCount > 2147483647)
     {
         throw new ArgumentException("Too many nodes: " + redNodeCount + ";" + blueNodeCount);
     }
     RedNodeCount  = redNodeCount;
     BlueNodeCount = blueNodeCount;
     Directed      = (directedness == Directedness.Directed);
 }
Exemple #4
0
        /// Loads from a reader.
        /// \param reader A reader on the input file, e.g. a StreamReader.
        /// \param directedness Specifies the directedness of the graph to be loaded. Possible values:
        /// - \c Directedness.Directed: each created arc will be directed.
        /// - \c Directedness.Undirected: each created arc will be an edge (i.e. undirected).
        /// \return the loaded nodes, by index ascending
        public Node[] Load(TextReader reader, Directedness directedness)
        {
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();

            string[] tokens;
            var      whitespaces = new Regex(@"\s+");

            // first line: number of nodes and arcs
            tokens = whitespaces.Split(reader.ReadLine());
            int nodeCount = int.Parse(tokens[0], CultureInfo.InvariantCulture);
            int arcCount  = int.Parse(tokens[1], CultureInfo.InvariantCulture);

            Node[] nodes = new Node[nodeCount];
            for (int i = 0; i < nodeCount; i++)
            {
                nodes[i] = buildableGraph.AddNode();
            }

            Extensions.Clear();

            for (int i = 0; i < arcCount; i++)
            {
                tokens = whitespaces.Split(reader.ReadLine());
                int a = (int)(long.Parse(tokens[0], CultureInfo.InvariantCulture) - StartIndex);
                int b = (int)(long.Parse(tokens[1], CultureInfo.InvariantCulture) - StartIndex);

                Arc arc = buildableGraph.AddArc(nodes[a], nodes[b], directedness);

                int extensionCount = tokens.Length - 2;
                for (int j = 0; j < extensionCount - Extensions.Count; j++)
                {
                    Extensions.Add(new Dictionary <Arc, string>());
                }
                for (int j = 0; j < extensionCount; j++)
                {
                    Extensions[j][arc] = tokens[2 + j];
                }
            }

            return(nodes);
        }
Exemple #5
0
        public virtual Arc AddArc(Node u, Node v, Directedness directedness)
        {
            if (ArcCount() == int.MaxValue)
            {
                throw new InvalidOperationException("Error: too many arcs!");
            }


            // check if u and v are valid nodes of the graph
            if (!(HasNode(u) && HasNode(v)))
            {
                throw new InvalidOperationException($"One of nodes {u.Id} or {v.Id} does not belong to this graph.");
            }

            Arc a = new Arc(arcAllocator.Allocate());

            arcs.Add(a);
            bool isEdge = (directedness == Directedness.Undirected);

            arcProperties[a] = new ArcProperties(u, v, isEdge);

            Utils.MakeEntry(nodeArcs_All, u).Add(a);
            Utils.MakeEntry(nodeArcs_Forward, u).Add(a);
            Utils.MakeEntry(nodeArcs_Backward, v).Add(a);

            if (isEdge)
            {
                edges.Add(a);
                Utils.MakeEntry(nodeArcs_Edge, u).Add(a);
            }

            if (v != u)
            {
                Utils.MakeEntry(nodeArcs_All, v).Add(a);
                if (isEdge)
                {
                    Utils.MakeEntry(nodeArcs_Edge, v).Add(a);
                    Utils.MakeEntry(nodeArcs_Forward, v).Add(a);
                    Utils.MakeEntry(nodeArcs_Backward, u).Add(a);
                }
            }

            return(a);
        }
        public CompleteGraph(int nodeCount, Directedness directedness)
        {
            this.nodeCount = nodeCount;
            Directed       = (directedness == Directedness.Directed);
            if (nodeCount < 0)
            {
                throw new ArgumentException("Invalid node count: " + nodeCount);
            }
            long num = (long)nodeCount * (long)(nodeCount - 1);

            if (!Directed)
            {
                num /= 2;
            }
            if (num > 2147483647)
            {
                throw new ArgumentException("Too many nodes: " + nodeCount);
            }
        }
Exemple #7
0
        public CompleteGraph(int nodeCount, Directedness directedness)
        {
            this.nodeCount = nodeCount;
            Directed       = (directedness == Directedness.Directed);

            if (nodeCount < 0)
            {
                throw new ArgumentException("Invalid node count: " + nodeCount);
            }
            long arcCount = (long)nodeCount * (nodeCount - 1);

            if (!Directed)
            {
                arcCount /= 2;
            }
            if (arcCount > int.MaxValue)
            {
                throw new ArgumentException("Too many nodes: " + nodeCount);
            }
        }
Exemple #8
0
        public Arc AddArc(Node u, Node v, Directedness directedness)
        {
            if (ArcCount() == int.MaxValue)
            {
                throw new InvalidOperationException("Error: too many arcs!");
            }

#if DEBUG
            // check if u and v are valid nodes of the graph
            System.Diagnostics.Debug.Assert(HasNode(u));
            System.Diagnostics.Debug.Assert(HasNode(v));
#endif

            Arc a = new Arc(arcAllocator.Allocate());
            arcs.Add(a);
            bool isEdge = (directedness == Directedness.Undirected);
            arcProperties[a] = new ArcProperties(u, v, isEdge);

            Utils.MakeEntry(nodeArcs_All, u).Add(a);
            Utils.MakeEntry(nodeArcs_Forward, u).Add(a);
            Utils.MakeEntry(nodeArcs_Backward, v).Add(a);

            if (isEdge)
            {
                edges.Add(a);
                Utils.MakeEntry(nodeArcs_Edge, u).Add(a);
            }

            if (v != u)
            {
                Utils.MakeEntry(nodeArcs_All, v).Add(a);
                if (isEdge)
                {
                    Utils.MakeEntry(nodeArcs_Edge, v).Add(a);
                    Utils.MakeEntry(nodeArcs_Forward, v).Add(a);
                    Utils.MakeEntry(nodeArcs_Backward, u).Add(a);
                }
            }

            return(a);
        }
Exemple #9
0
        public Node[] Load(TextReader reader, Directedness directedness)
        {
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();
            Regex regex = new Regex("\\s+");

            string[] array = regex.Split(reader.ReadLine());
            int      num   = int.Parse(array[0], CultureInfo.InvariantCulture);
            int      num2  = int.Parse(array[1], CultureInfo.InvariantCulture);

            Node[] array2 = new Node[num];
            for (int i = 0; i < num; i++)
            {
                array2[i] = buildableGraph.AddNode();
            }
            Extensions.Clear();
            for (int j = 0; j < num2; j++)
            {
                array = regex.Split(reader.ReadLine());
                int num3 = (int)(long.Parse(array[0], CultureInfo.InvariantCulture) - StartIndex);
                int num4 = (int)(long.Parse(array[1], CultureInfo.InvariantCulture) - StartIndex);
                Arc key  = buildableGraph.AddArc(array2[num3], array2[num4], directedness);
                int num5 = array.Length - 2;
                for (int k = 0; k < num5 - Extensions.Count; k++)
                {
                    Extensions.Add(new Dictionary <Arc, string>());
                }
                for (int l = 0; l < num5; l++)
                {
                    Extensions[l][key] = array[2 + l];
                }
            }
            return(array2);
        }
Exemple #10
0
        public Arc AddArc(Node u, Node v, Directedness directedness)
        {
            if (ArcCount() == int.MaxValue)
            {
                throw new InvalidOperationException("Error: too many arcs!");
            }
            Arc a = new Arc(arcAllocator.Allocate());

            arcs.Add(a);
            bool isEdge = (directedness == Directedness.Undirected);

            arcProperties[a] = new ArcProperties(u, v, isEdge);

            Utils.MakeEntry(nodeArcs_All, u).Add(a);
            Utils.MakeEntry(nodeArcs_Forward, u).Add(a);
            Utils.MakeEntry(nodeArcs_Backward, v).Add(a);

            if (isEdge)
            {
                edges.Add(a);
                Utils.MakeEntry(nodeArcs_Edge, u).Add(a);
            }

            if (v != u)
            {
                Utils.MakeEntry(nodeArcs_All, v).Add(a);
                if (isEdge)
                {
                    Utils.MakeEntry(nodeArcs_Edge, v).Add(a);
                    Utils.MakeEntry(nodeArcs_Forward, v).Add(a);
                    Utils.MakeEntry(nodeArcs_Backward, u).Add(a);
                }
            }

            return(a);
        }
Exemple #11
0
        /// Loads from an XML document.
        public void Load(XDocument doc)
        {
            // Namespaces are ignored so we can load broken documents.
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();
            XElement xGraphML = doc.Root;

            // load properties
            Properties.Clear();
            Dictionary <string, GraphMLProperty> propertyById = new Dictionary <string, GraphMLProperty>();

            foreach (var xKey in Utils.ElementsLocal(xGraphML, "key"))
            {
                foreach (var handler in PropertyLoaders)
                {
                    try
                    {
                        GraphMLProperty p = handler(xKey);
                        Properties.Add(p);
                        propertyById[p.Id] = p;
                        break;
                    }
                    catch (ArgumentException) { }
                }
            }

            // load graph
            XElement     xGraph = Utils.ElementLocal(xGraphML, "graph");
            Directedness defaultDirectedness = (xGraph.Attribute("edgedefault").Value == "directed" ?
                                                Directedness.Directed : Directedness.Undirected);

            ReadProperties(propertyById, xGraph, Graph);
            // load nodes
            Dictionary <string, Node> nodeById = new Dictionary <string, Node>();

            foreach (var xNode in Utils.ElementsLocal(xGraph, "node"))
            {
                Node node = buildableGraph.AddNode();
                nodeById[xNode.Attribute("id").Value] = node;
                ReadProperties(propertyById, xNode, node);
            }
            // load arcs
            foreach (var xArc in Utils.ElementsLocal(xGraph, "edge"))
            {
                Node u = nodeById[xArc.Attribute("source").Value];
                Node v = nodeById[xArc.Attribute("target").Value];

                Directedness dir     = defaultDirectedness;
                XAttribute   dirAttr = xArc.Attribute("directed");
                if (dirAttr != null)
                {
                    dir = (dirAttr.Value == "true" ? Directedness.Directed : Directedness.Undirected);
                }

                Arc arc = buildableGraph.AddArc(u, v, dir);
                ReadProperties(propertyById, xArc, arc);
            }
        }
        public void Load(XDocument doc)
        {
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();
            XElement root = doc.Root;

            Properties.Clear();
            Dictionary <string, GraphMLProperty> dictionary = new Dictionary <string, GraphMLProperty>();

            foreach (XElement item in Utils.ElementsLocal(root, "key"))
            {
                using (List <Func <XElement, GraphMLProperty> > .Enumerator enumerator2 = PropertyLoaders.GetEnumerator())
                {
                    while (true)
                    {
                        if (enumerator2.MoveNext())
                        {
                            Func <XElement, GraphMLProperty> current2 = enumerator2.Current;
                            try
                            {
                                GraphMLProperty graphMLProperty = current2(item);
                                Properties.Add(graphMLProperty);
                                dictionary[graphMLProperty.Id] = graphMLProperty;
                            }
                            catch (ArgumentException)
                            {
                                continue;
                            }
                        }
                        break;
                    }
                }
            }
            XElement     xElement     = Utils.ElementLocal(root, "graph");
            Directedness directedness = (!(xElement.Attribute("edgedefault").Value == "directed")) ? Directedness.Undirected : Directedness.Directed;

            ReadProperties(dictionary, xElement, Graph);
            Dictionary <string, Node> dictionary2 = new Dictionary <string, Node>();

            foreach (XElement item2 in Utils.ElementsLocal(xElement, "node"))
            {
                Node node = buildableGraph.AddNode();
                dictionary2[item2.Attribute("id").Value] = node;
                ReadProperties(dictionary, item2, node);
            }
            foreach (XElement item3 in Utils.ElementsLocal(xElement, "edge"))
            {
                Node         u             = dictionary2[item3.Attribute("source").Value];
                Node         v             = dictionary2[item3.Attribute("target").Value];
                Directedness directedness2 = directedness;
                XAttribute   xAttribute    = item3.Attribute("directed");
                if (xAttribute != null)
                {
                    directedness2 = ((!(xAttribute.Value == "true")) ? Directedness.Undirected : Directedness.Directed);
                }
                Arc arc = buildableGraph.AddArc(u, v, directedness2);
                ReadProperties(dictionary, item3, arc);
            }
        }
Exemple #13
0
 public PathGraph(int nodeCount, Topology topology, Directedness directedness)
 {
     this.nodeCount = nodeCount;
     isCycle        = (topology == Topology.Cycle);
     directed       = (directedness == Directedness.Directed);
 }
Exemple #14
0
        /// Loads from a reader.
        /// \param reader A reader on the input file, e.g. a StreamReader.
        /// \param directedness Specifies the directedness of the graph to be loaded. Possible values:
        /// - \c Directedness.Directed: each created arc will be directed.
        /// - \c Directedness.Undirected: each created arc will be undirected.
        /// - \c null (default): arcs defined in \c \@arcs sections will be directed,
        ///   while those defined in \c \@edges sections will be undirected.
        public void Load(TextReader reader, Directedness?directedness)
        {
            if (Graph == null)
            {
                Graph = new CustomGraph();
            }
            IBuildableGraph buildableGraph = (IBuildableGraph)Graph;

            buildableGraph.Clear();

            NodeMaps.Clear();
            var nodeFromLabel = new Dictionary <string, Node>();

            ArcMaps.Clear();
            Attributes.Clear();

            Regex         splitRegex       = new Regex(@"\s*((""(\""|.)*"")|(\S+))\s*", RegexOptions.Compiled);
            string        section          = "";
            Directedness  currDir          = Directedness.Directed;   // are currently read arcs directed?
            bool          prevHeader       = false;
            List <string> columnNames      = null;
            int           labelColumnIndex = -1;

            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                line = line.Trim();
                if (line == "" || line[0] == '#')
                {
                    continue;
                }
                List <string> tokens = splitRegex.Matches(line).Cast <Match>()
                                       .Select(m =>
                {
                    string s = m.Groups[1].Value;
                    if (s == "")
                    {
                        return(s);
                    }
                    if (s[0] == '"' && s[s.Length - 1] == '"')
                    {
                        s = Unescape(s.Substring(1, s.Length - 2));
                    }
                    return(s);
                }).ToList();
                string first = tokens.First();

                // header?
                if (line[0] == '@')
                {
                    section = first.Substring(1);
                    currDir = directedness ?? (section == "arcs" ? Directedness.Directed : Directedness.Undirected);

                    prevHeader = true;
                    continue;
                }

                switch (section)
                {
                case "nodes":
                case "red_nodes":
                case "blue_nodes":
                {
                    if (prevHeader)
                    {
                        columnNames = tokens;
                        for (int i = 0; i < columnNames.Count; i++)
                        {
                            string column = columnNames[i];
                            if (column == "label")
                            {
                                labelColumnIndex = i;
                            }
                            if (!NodeMaps.ContainsKey(column))
                            {
                                NodeMaps[column] = new Dictionary <Node, string>();
                            }
                        }
                    }
                    else
                    {
                        Node node = buildableGraph.AddNode();
                        for (int i = 0; i < tokens.Count; i++)
                        {
                            NodeMaps[columnNames[i]][node] = tokens[i];
                            if (i == labelColumnIndex)
                            {
                                nodeFromLabel[tokens[i]] = node;
                            }
                        }
                    }
                } break;

                case "arcs":
                case "edges":
                {
                    if (prevHeader)
                    {
                        columnNames = tokens;
                        foreach (var column in columnNames)
                        {
                            if (!ArcMaps.ContainsKey(column))
                            {
                                ArcMaps[column] = new Dictionary <Arc, string>();
                            }
                        }
                    }
                    else
                    {
                        Node u   = nodeFromLabel[tokens[0]];
                        Node v   = nodeFromLabel[tokens[1]];
                        Arc  arc = buildableGraph.AddArc(u, v, currDir);
                        for (int i = 2; i < tokens.Count; i++)
                        {
                            ArcMaps[columnNames[i - 2]][arc] = tokens[i];
                        }
                    }
                } break;

                case "attributes":
                {
                    Attributes[tokens[0]] = tokens[1];
                } break;
                }
                prevHeader = false;
            }             // while can read from file
        }