예제 #1
0
 public Edge(Edge ed)
 {
     source  = ed.source;
     destiny = ed.destiny;
     weight  = ed.Weight;
     visited = ed.Visited;
 }
예제 #2
0
        private bool EulerCircuit(NodeP actual, NodeP origin)
        {
            bool resp = false;

            actual.Visited = true;
            if (actual.Name == origin.Name && actual.Visited && graph.AllEdgesVisited())
            {
                tmpNodes.Add(actual);
                return(true);
            }

            foreach (NodeR nrel in actual.relations)
            {
                Edge a = graph.GetEdge(nrel.Up, actual);

                if (a != null && !a.Visited)
                {
                    a.Visited = true;
                    resp     |= EulerCircuit(nrel.Up, origin);
                    if (!resp)
                    {
                        a.Visited = false;
                    }
                    else
                    {
                        actual.Visited = true;
                        tmpNodes.Add(actual);
                        break;
                    }
                }
            }
            return(resp);
        }
예제 #3
0
        public void searchInBreadth(Graph graph, NodeP v)
        {
            Queue queue = new Queue();
            NodeP x, y;

            x = new NodeP();
            y = new NodeP();

            v.Visited = true;
            queue.Enqueue(v);
            while (queue.Count > 0)
            {
                x = (NodeP)queue.Dequeue();
                g.AddNode(x);
                foreach (NodeR nodeR in x.relations)
                {
                    if (!nodeR.Up.Visited)
                    {
                        nodeR.Up.Visited = true;
                        queue.Enqueue(nodeR.Up);
                        g.AddNode(nodeR.Up);
                        Edge edge = new Edge(x, nodeR.Up, "e" + g.EdgesList.Count.ToString());
                        g.AddEdge(edge);
                    }
                }
            }
        }
예제 #4
0
        public double [] Dijkstra(Graph graph, List <Edge> edges, NodeP source)
        {
            double[][] C = Weights(graph.Count, edges);
            double[]   D = new double[graph.Count];
            string     S = "";
            NodeP      w;

            graph[ConverterWord(source.Name)].Visited = true;
            S += source.Name;

            for (int i = 0; i < D.Length; i++)
            {
                D[i] = C[ConverterWord(source.Name)][i];
            }


            for (int i = 0; i < graph.Count - 1; i++)
            {
                w         = MinEdge(graph, D, source);
                S        += w.Name;
                w.Visited = true;

                foreach (NodeP n in graph)
                {
                    if (!n.Visited && graph.Connected(w, n) != null)
                    {
                        D[ConverterWord(n.Name)] = Min(D[ConverterWord(n.Name)], D[ConverterWord(w.Name)] + C[ConverterWord(w.Name)][ConverterWord(n.Name)]);
                    }
                }
            }

            return(D);
        }
예제 #5
0
        public bool EulerRoad(NodeP actual, NodeP origin)
        {
            bool resp = false;

            if (graph.AllEdgesVisited())
            {
                tmpNodes.Add(actual);
                return(true);
            }

            foreach (NodeR nrel in actual.relations)
            {
                Edge a = graph.GetEdge(nrel.Up, actual);

                if (a != null && !a.Visited)
                {
                    a.Visited = true;
                    resp     |= EulerRoad(nrel.Up, origin);
                    if (!resp)
                    {
                        a.Visited = false;
                    }
                    else
                    {
                        actual.Visited = true;
                        tmpNodes.Add(actual);
                        break;
                    }
                }
            }
            return(resp);
        }
예제 #6
0
 public Edge(Edge ed)
 {
     origin  = ed.origin;
     destiny = ed.destiny;
     weight  = ed.Weight;
     visited = ed.Visited;
 }
예제 #7
0
 public void bpf(NodeP v, int counter, int treeNumber, Graph g)
 {
     v.TreeNumber = treeNumber;
     v.Visited    = true;
     v.Depth      = counter;
     counter++;
     foreach (NodeR w in v.relations)
     {
         if (!w.Up.Visited)
         {
             Edge edge = new Edge(v, w.Up, "e" + g.EdgesList.Count.ToString());
             g.treeList.Add(edge);
             bpf(w.Up, counter, treeNumber, g);
         }
         else if (w.Up.Depth - v.Depth > 0 && w.Up.TreeNumber == v.TreeNumber)
         {
             Edge edge = new Edge(v, w.Up, "e" + g.EdgesList.Count.ToString());
             g.advanceList.Add(edge);
         }
         else if (w.Up.Depth - v.Depth < 0 && w.Up.TreeNumber == v.TreeNumber)
         {
             Edge edge = new Edge(v, w.Up, "e" + g.EdgesList.Count.ToString());
             g.backList.Add(edge);
         }
         else if (w.Up.Depth - v.Depth == 0 || w.Up.TreeNumber != v.TreeNumber)
         {
             Edge edge = new Edge(v, w.Up, "e" + g.EdgesList.Count.ToString());
             g.crossoverList.Add(edge);
         }
     }
 }
예제 #8
0
 private bool verifCircuito3(NodeP n)
 {
     foreach (NodeR r2 in n.relations)
     {
         NodeP n2 = r2.Up;
         if (n2.Name != n.Name)
         {
             foreach (NodeR r3 in n2.relations)
             {
                 if (r3.Name != n.Name)
                 {
                     NodeP n3 = r3.Up;
                     foreach (NodeR r4 in n3.relations)
                     {
                         if (r4.Up == n)
                         {
                             return(true);
                         }
                     }
                 }
             }
         }
     }
     return(false);
 }
예제 #9
0
 private void removeBridges(Graph g2)
 {
     for (int i = 0; i < g2.Count; i++)
     {
         if (g2[i].Degree == 2)
         {
             //creapuente2nodos(g2[i]);
             NodeP a    = g2[i].relations[0].Up;
             NodeP b    = g2[i].relations[1].Up;
             Edge  ed   = new Edge(a, b, "p");
             bool  esta = false;
             foreach (Edge eee in g2.EdgesList)
             {
                 //si ya esta
                 if ((ed.Origin.Name == eee.Origin.Name && ed.Destiny.Name == eee.Destiny.Name) || (ed.Origin.Name == eee.Destiny.Name && ed.Destiny.Name == eee.Origin.Name))
                 {
                     esta = true;
                 }
             }
             if (!esta)
             {
                 g2.EdgesList.Add(ed);
                 a.InsertRelation(b, 1, false);
                 b.InsertRelation(a, 1, false);
             }
             g2.RemoveNode(g2[i--]);
         }
     }
 }
예제 #10
0
 public Edge(NodeP origin, NodeP destiny, string name)
 {
     this.source  = origin;
     this.destiny = destiny;
     this.name    = name;
     this.weight  = 0;
     this.visited = false;
 }
예제 #11
0
        private List <NodeP> getSurroundNodes(NodeP nod)
        {
            List <NodeP> tempRel = new List <NodeP>();

            foreach (NodeR rel in nod.relations)
            {
                tempRel.Add(rel.Up);
            }
            return(tempRel);
        }
예제 #12
0
        private NodeP MinEdge(Graph graph, double[] D, NodeP source)
        {
            NodeP w = graph[0];//!= source ? graph[0] : graph[1];

            foreach (NodeP n in graph)
            {
                w = !n.Visited && D[ConverterWord(n.Name)] < D[ConverterWord(w.Name)] || w.Visited ? n : w;
            }
            return(w);
        }
예제 #13
0
 public NodeP(NodeP co)
 {
     position  = co.Position;
     name      = co.Name;
     relations = new List <NodeR>();
     degree    = co.Degree;
     degreeEx  = co.DegreeEx;
     degreeIn  = co.DegreeIn;
     color     = co.Color;
     selected  = false;
 }
예제 #14
0
        public void InsertRelation(NodeP newRel, int num, bool isDirected)
        {
            Degree++;
            if (isDirected)
            {
                DegreeEx++;
                newRel.DegreeIn++;
            }

            relations.Add(new NodeR(newRel, "e" + num.ToString()));
        }
예제 #15
0
 // regresa true si hay un nodo que pertenece a un grupo de nodos, pertenece al metodo n partita
 private bool nodoDentroGrupo(List <NodeP> g1, NodeP nn)
 {
     foreach (NodeP ng in g1)
     {
         if (ng.Name == nn.Name)
         {
             return(true);
         }
     }
     return(false);
 }
예제 #16
0
 private bool nodesInsideGroup(List <Edge> liEd, NodeP a, NodeP b)
 {
     foreach (Edge ed in liEd)
     {
         if ((a.Name == ed.Origin.Name || a.Name == ed.Destiny.Name) ||
             (b.Name == ed.Origin.Name || b.Name == ed.Destiny.Name))
         {
             return(true);
         }
     }
     return(false);
 }
예제 #17
0
 public void topoprueba(NodeP actual)
 {
     actual.Visited = true;
     foreach (NodeR rel in actual.relations)
     {
         if (rel.Up.Visited == false)
         {
             topoprueba(rel.Up);
         }
     }
     tmpNodes.Add(actual);
 }
예제 #18
0
        public void EncuentraSig(NodeP np)
        {
            foreach (Edge ar in gra.EdgesList)
            {
                if (ar.Origin == np && !ar.Destiny.Visited)
                {
                    listView2.Items.Add(ar.Destiny.Name);
                    ar.Destiny.Visited = true;

                    EncuentraSig(ar.Destiny);
                }
            }
        }
예제 #19
0
        public bool makeHamilton(NodeP origen, NodeP actual)
        {
            bool resp = false;

            actual.Visited = true;
            if (graph.AllNodesVisited())
            {
                tmpNodes.Add(actual);
                return(true);
            }

            foreach (NodeR nrel in actual.relations)
            {
                Edge a = graph.GetEdge(nrel.Up, actual);
                if (!a.Visited)
                {
                    a.Visited = true;
                    if (actual == a.Origin && !a.Destiny.Visited)
                    {
                        resp |= makeHamilton(origen, a.Destiny);
                        if (!resp)
                        {
                            a.Destiny.Visited = false;
                        }
                        else
                        {
                            tmpNodes.Add(a.Origin);
                            break;
                        }
                    }
                    else if (actual == a.Destiny && !a.Origin.Visited)
                    {
                        resp |= makeHamilton(origen, a.Origin);
                        if (!resp)
                        {
                            a.Origin.Visited = false;
                        }
                        else
                        {
                            tmpNodes.Add(a.Destiny);
                            break;
                        }
                    }
                }
                if (!resp)
                {
                    a.Visited = false;
                }
            }
            return(resp);
        }
예제 #20
0
        private void clasificacionTopologica(NodeP v, List <NodeP> g)
        {
            v.Visited = true;
            foreach (NodeR rel in v.relations)
            {
                if (rel.Up.Visited != true)
                {
                    clasificacionTopologica(rel.Up, g);
                }
            }
            NodeP nuevo = new NodeP(v);

            g.Add(nuevo);
        }
예제 #21
0
 private void deepSearch(NodeP node)
 {
     node.Visited = true;
     foreach (NodeR nodeR in node.relations)
     {
         if (nodeR.Up.Visited == false)
         {
             g.AddNode(nodeR.Up);
             Edge edge = new Edge(node, nodeR.Up, "e" + g.EdgesList.Count.ToString());
             g.AddEdge(edge);
             q.Enqueue(nodeR.Up.Name);
             deepSearch(nodeR.Up);
         }
     }
 }
예제 #22
0
 // Regresa true si hay una arista que pertenece a un grupo de nodos, pertenece al metodo n partita
 private bool aristaDentroGrupo(List <NodeP> g1, NodeP nn)
 {
     foreach (NodeP ng in g1)
     {
         foreach (Edge ed in graph.EdgesList)
         {
             if ((ed.Origin.Name == nn.Name && ed.Destiny.Name == ng.Name) ||
                 (ed.Origin.Name == ng.Name && ed.Destiny.Name == nn.Name))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
예제 #23
0
        public void eliminaNodo(NodeP np)
        {
            foreach (NodeP no in gra)
            {
                no.Visited = false;
            }

            listView2.Items.Clear();
            comboBox1.Items.Remove(np.Name);
            listView1.Items.Add(np.Name);

            EncuentraSig(np);

            gra.RemoveNode(np);
            editor.Invalidate();
        }
예제 #24
0
        public List <NodeP> Topological(NodeP nod)
        {
            graph.UnselectAllNodes();

            List <NodeP> clasificacion = new List <NodeP>();

            foreach (NodeP nodo in graph)
            {
                if (nodo.Visited != true)
                {
                    clasificacionTopologica(nodo, clasificacion);
                }
                else
                {
                }
            }
            return(clasificacion);
        }
예제 #25
0
        private void tryEveryNodeK33(Graph g2, List <NodeP> nods)
        {
            List <NodeP>         tempRel;
            List <List <NodeP> > grupos = null;
            NodeP tempNode;

            if (checkHomeomorphicK33(g2))
            {
                grupos = equalToK33(g2);
                if (grupos != null && nods.Count == 0)
                {
                    foreach (List <NodeP> lnp in grupos)
                    {
                        foreach (NodeP np in lnp)
                        {
                            nods.Add(np);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < g2.Count; i++)
                    {
                        tempRel  = getSurroundNodes(g2[i]);
                        tempNode = new NodeP(g2[i]);
                        g2.RemoveNode(g2[i]);

                        // llamada recursiva
                        tryEveryNodeK33(g2, nods);
                        // unir nodos otra vez
                        tempNode.Degree = 0;
                        g2.Insert(i, tempNode);
                        //g2.Add(tempNode);
                        for (int j = 0; j < tempRel.Count; j++)
                        {
                            tempRel[j].InsertRelation(tempNode, j, false);
                            tempNode.InsertRelation(tempRel[j], j, false);
                            g2.EdgesList.Add(new Edge(tempNode, tempRel[j], "e" + j.ToString()));
                        }
                    }
                }
            }
        }
예제 #26
0
        public string CircuitEuler(Graph graph, NodeP node)
        {
            if (!CheckDegree(graph))
            {
                return("No Existe Un Circuito Euleriano");
            }

            List <List <int> > ListAdyacencia = MatrizAd(graph);
            int    i, j, anterior = 0;
            string path = "", auxPath = "";
            NodeP  sourceNode = node;

            while (sourceNode != null)
            {
                i = ConverterWord(sourceNode.Name);

                auxPath = sourceNode.Name;

                for (j = 0; j < graph.Count; j++)
                {
                    if (ListAdyacencia[i][j] == 1)
                    {
                        ListAdyacencia[i][j] = ListAdyacencia[j][i] = 0;

                        i        = j;
                        auxPath += graph[i].Name;
                        j        = -1;

                        if (graph[i].Equals(sourceNode))
                        {
                            break;
                        }
                    }
                }
                path       = JoinPath(path, auxPath);
                sourceNode = NextNode(ListAdyacencia, path) >= 0 ? graph[NextNode(ListAdyacencia, path)] : null;
            }

            return(path);
        }
예제 #27
0
        private void FindEulerCycleRoad(Edge e, NodeP orig, NodeP dest, List <Edge> euler)
        {
            e.Visited = true;
            euler.Add(e);

            //busca la siguiente arista conectada al nodo actual en la lista de aristas
            bool resp = graph.AllEdgesVisited();

            if (!resp)
            {
                foreach (Edge currEd in graph.EdgesList)
                {
                    if (!currEd.Visited && currEd.Destiny.Name == dest.Name)
                    {
                        FindEulerCycleRoad(currEd, currEd.Destiny, currEd.Origin, euler);
                    }
                    if (!currEd.Visited && currEd.Origin.Name == dest.Name)
                    {
                        FindEulerCycleRoad(currEd, currEd.Origin, currEd.Destiny, euler);
                    }
                }
            }
        }
예제 #28
0
 public NodeR(NodeP np, string nam)
 {
     up      = np;
     visited = false;
     name    = nam;
 }
예제 #29
0
 public void SetNodeP(NodeP node)
 {
     NodeList.Add(node);
 }
예제 #30
0
        private void tryNodesDegree5(Graph g2, NodeP np, List <Edge> arist)
        {
            // Hace la prueba para cada arista del nodo
            for (int i = 0; i < np.relations.Count; i++)
            {
                Edge ed = g2.GetEdge(np, np.relations[i].Up);

                g2.RemoveEdge(ed);


                if (checkHomeomorphicK5(g2))
                {
                    if (np == ed.Origin)
                    {
                        if (ed.Destiny.Visited)
                        {
                            if (ed.Destiny.Degree >= 4 && !ed.Origin.Visited)
                            {
                                arist.Add(ed);
                            }
                        }
                        else
                        {
                            arist.Add(ed);
                        }
                    }
                    else
                    {
                        if (np == ed.Destiny)
                        {
                            if (ed.Origin.Visited)
                            {
                                if (ed.Origin.Degree >= 4 && !ed.Destiny.Visited)
                                {
                                    arist.Add(ed);
                                }
                            }
                            else
                            {
                                arist.Add(ed);
                            }
                        }
                    }
                }

                // la vuelve a agregar

                g2.EdgesList.Insert(i, ed);
                if (np == ed.Origin)
                {
                    np.Degree++;
                    ed.Destiny.Degree++;
                    np.relations.Insert(i, new NodeR(ed.Destiny, "e" + i.ToString()));
                    ed.Destiny.relations.Add(new NodeR(np, "e" + i.ToString()));
                }
                else
                {
                    np.Degree++;
                    ed.Origin.Degree++;
                    np.relations.Insert(i, new NodeR(ed.Origin, "e" + i.ToString()));
                    ed.Origin.relations.Add(new NodeR(np, "e" + i.ToString()));
                }
            }
        }