Esempio n. 1
0
        public EdgeList matchingStringsToEdgeList(BipartiteGraph G, List <string> M_String)
        {
            var       M_to_EdgeListM = new EdgeList();
            GraphNode nd1, nd2;

            foreach (string edg in M_String)
            {
                nd1 = G.GetNode(edg.Split('-')[0]);
                nd2 = G.GetNode(edg.Split('-')[1]);
                if (G.Contains(nd1, nd2))
                {
                    M_to_EdgeListM.Add(G.Edges.FindByNodes(nd1, nd2));
                }
                else if (G.Contains(nd2, nd1))
                {
                    M_to_EdgeListM.Add(G.Edges.FindByNodes(nd2, nd1));
                }
                //else
                //{
                //    M_to_EdgeListM.Add(new Edge(nd1,nd2,0));
                //}
            }

            return(M_to_EdgeListM);
        }
Esempio n. 2
0
        //taken from enumeratingmatchinginbipartiteGraph class.... decide where to keep this method here or in the 'enumeratingmathcihgin...' class
        public EdgeList ToEdges(List <GraphNode> cycle)
        {
            var elist = new EdgeList();

            for (int i = 1; i < cycle.Count; i++)
            {
                Edge edg = Edges.FindByNodes(cycle[cycle.Count - i], cycle[cycle.Count - i - 1]);
                elist.Add(edg);
            }
            return(elist);
        }
        public static EdgeList Get(BipartiteGraph bipartiteGraph)
        {
            NodeList models = bipartiteGraph.ModelsSet;

            if (models is null)
            {
                throw new ArgumentException("Graph has no models", nameof(bipartiteGraph));
            }

            NodeList variables = bipartiteGraph.VariablesSet;

            if (variables is null)
            {
                throw new ArgumentException("Graph has no variables", nameof(bipartiteGraph));
            }

            //Assigning int values to each node in the graph starting from 0.
            var modNodeIntMap = models.Select((m, i) => new KeyValuePair <string, int>(m.Value, i)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            //Creating Adjacency List
            var adjacencyList = new List <int[]>(variables.Count);

            foreach (GraphNode variable in variables)
            {
                adjacencyList.Add(variable.Neighbors.Select(n => modNodeIntMap[n.Value]).ToArray());
            }

            // Get Match
            IList <int> match = new HopcroftKarpClass(adjacencyList, models.Count).GetMatching();

            // Format the match
            var MatchingEdgeList = new EdgeList();
            int idx = 0;

            foreach (Node variable in variables)
            {
                int matchedModelIndex = match[idx];
                if (matchedModelIndex != -1)
                {
                    string modelValue = modNodeIntMap.FirstOrDefault(x => x.Value == matchedModelIndex).Key;
                    //matching edge list contains edges having direction from
                    MatchingEdgeList.Add(bipartiteGraph.Edges.FindByNodes(variable, bipartiteGraph.GetNode(modelValue)));
                    // 'variables set' to 'model set'
                    idx++;
                }
                else
                {
                    idx++; /*variable associated at that location is unmatched*/                   //Implement to store unmatched variable information if required
                }
            }

            return(MatchingEdgeList);
        }
        //This method find the corresponding edges of matching M(which contains edges from other Graph) for the given Graph G
        private static EdgeList CorrespondingEdgeList(BipartiteGraph G, EdgeList M)
        {
            var corresponding = new EdgeList();

            foreach (Edge edge in M)
            {
                GraphNode from = G.GetNode(edge.FromNode.Value);
                GraphNode to   = G.GetNode(edge.ToNode.Value);
                if (from == null && to == null)
                {
                    continue;
                }

                if (G.Edges.FindByNodes(from, to) != null)
                {
                    corresponding.Add(G.Edges.FindByNodes(from, to));
                }
                else
                {
                    corresponding.Add(G.Edges.FindByNodes(to, from));
                }
            }
            return(corresponding);
        }
        private EdgeList MatchingToEdgeList(BipartiteGraph G, MatchingList M_String)
        {
            var edgeList = new EdgeList();

            foreach (string edg in M_String)
            {
                GraphNode node1 = G.GetNode(edg.Split('-')[0]);
                GraphNode node2 = G.GetNode(edg.Split('-')[1]);
                if (G.Contains(node1, node2))
                {
                    edgeList.Add(G.Edges.FindByNodes(node1, node2));
                }
                else if (G.Contains(node2, node1))
                {
                    edgeList.Add(G.Edges.FindByNodes(node2, node1));
                }
                //else
                //{
                //    M_to_EdgeListM.Add(new Edge(nd1,nd2,0));
                //}
            }

            return(edgeList);
        }
Esempio n. 6
0
        private static EdgeList CorrespondingListForTheGraph(BipartiteGraph G, EdgeList M) //This method find the corresponding edges of matching M(which contains edges from other Graph) for the given Graph G
        {
            var M_corresp = new EdgeList();

            foreach (Edge edg in M)
            {
                GraphNode frm = G.GetNode(edg.FromNode.Value);
                GraphNode to  = G.GetNode(edg.ToNode.Value);
                if (frm == null && to == null)
                {
                    goto xyz;
                }
                if (G.Edges.FindByNodes(frm, to) != null)
                {
                    M_corresp.Add(G.Edges.FindByNodes(frm, to));
                }
                else
                {
                    M_corresp.Add(G.Edges.FindByNodes(to, frm));
                }
                xyz :;
            }
            return(M_corresp);
        }