elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Problema con grafos
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problema con grafos  (Leído 1,603 veces)
Puma93

Desconectado Desconectado

Mensajes: 3


Ver Perfil
Problema con grafos
« en: 9 Junio 2013, 22:28 pm »

Hola a todos , tengo un problema para resolver

resulta que tengo un grafo, necesito un algoritmo que examine si un nodo esta dentro de un ciclo, en relidad he leido bastante, sobre ciclos hamiltonianos, eulerianos pero ninguno de estos me sirve ademas de que mire si las matrices de adyacencia me permitan ver si el nodo estaba en un ciclo pero nada, no se que hacer!
Código
  1.  


En línea

kasuki92

Desconectado Desconectado

Mensajes: 1


Ver Perfil
Re: Problema con grafos
« Respuesta #1 en: 20 Junio 2013, 22:50 pm »

oyo casualmente yo tuve que hacer algo parecido algo parecido para un trabajo.
lo que yo hago es buscar todos los caminos independientes del grafo y verifico cuando hay un ciclo para no quedarme en un ciclo infinito. lo que te quedaría con el código es sacar los nodos del camino que estan dentro del ciclo
te dejo el código tu correlo y dime si te sirvió de algo
El código ya tiene un grafo creado es grafo multienlazado no se como lo trabajas tú

De antemano te digo que esta complicado

//Método

public static LinkedList<LinkedList<Path>>independentPath(ILinkedWeightedEdgeDirectedGraph flowGraph){
      LinkedList<LinkedList<Path>> paths= new LinkedList<LinkedList<Path>>();
      LinkedList<Path> path=null;
      LinkedList<Path>path1=null;
      LinkedList<LinkedList<Path>>temporalPaths=null;
      Vertex v=flowGraph.getVerticesList().getFirst();
      int i=0;
      int k;
      int l;
      int count=0;
      int count1=0;
      boolean f;
      boolean z;
      for(Vertex ve:flowGraph.getVerticesList()){
         if(ve.getEdgeList().size()==2)
            count++;
      }
      do{
   
         path=new LinkedList<Path>();
         f=false;
         while(v.getEdgeList().size()!=0){
            z=false;
            k=0;
            
            if(v.getEdgeList().size()==2){
               if(((String)((WeightedEdge)v.getEdgeList().getFirst()).getWeight()).equals("true")){
                  Path p=new Path("true",v);
                  path.add(p);
                  l=path.size()-1;
                  while(k<path.size()&&!z){
                     Path p1=path.get(k);
                     if(v.getEdgeList().getFirst().getVertex().equals(p1.getVertex())){
                         while(l>=0 && !z){   
                        if(path.get(l).getPath()=="true"){   
                           Path pat=getPath(v.getEdgeList().getFirst().getVertex(),path);
                           for(int j=path.indexOf(pat);j<=l;j++){
                              path.add(path.get(j));
                           }
                           
                           Path p2=new Path("false",path.get(l).getVertex());
                           path.removeLast();
                           path.addLast(p2);
                           z=true;
                           if(((String)((WeightedEdge)path.getLast().getVertex().getEdgeList().getFirst()).getWeight()).equals("false")){
                              v=path.getLast().getVertex().getEdgeList().getFirst().getVertex();
                           }else
                              v=path.getLast().getVertex().getEdgeList().get(1).getVertex();
                         }
                        l--;
                        }
                     }
                        
                     k++;
                     
                  }
                  if(z==false)
                  v=v.getEdgeList().getFirst().getVertex();
               }else{
                  Path p=new Path("true",v);
                  path.add(p);
                  l=path.size()-1;
                  while(k<path.size()&&!z){
                     Path p1=path.get(k);
                     if(v.getEdgeList().get(1).getVertex().equals(p1.getVertex())){
                        while(l>=0 && !z){   
                           if(path.get(l).getPath()=="true"){   
                              Path pat=getPath(v.getEdgeList().getFirst().getVertex(),path);
                              for(int j=path.indexOf(pat);j<=l;j++){
                                 path.add(path.get(j));
                              }
                            Path p2=new Path("false",path.getLast().getVertex());
                           path.removeLast();
                           path.addLast(p2);
                           z=true;
                           if(((String)((WeightedEdge)path.getLast().getVertex().getEdgeList().getFirst()).getWeight()).equals("false")){
                              v=path.getLast().getVertex().getEdgeList().getFirst().getVertex();
                           }else
                              v=path.getLast().getVertex().getEdgeList().get(1).getVertex();
                           }
                           
                           l--;
                        }
                     }
                     k++;
                  }
                  if(z==false)
                  v=v.getEdgeList().get(1).getVertex();
               }
            }
            if(v.getEdgeList().size()==1){
               k=0;
               if(!path.isEmpty()){
                  l=path.size()-1;
                  while(k<path.size()&&!z){
                     Path p1=path.get(k);
                  if(v.getEdgeList().getFirst().getVertex().equals(p1.getVertex())){
                     while(l>=0 && !z){   
                        if(path.get(l).getPath()=="true"){
                           Path pat=getPath(v.getEdgeList().getFirst().getVertex(),path);
                           for(int j=path.indexOf(pat);j<=l;j++){
                              path.add(path.get(j));
                           }
                         Path p2=new Path("false",path.getLast().getVertex());
                        path.removeLast();
                        path.addLast(p2);
                        z=true;
                        if(((String)((WeightedEdge)path.getLast().getVertex().getEdgeList().getFirst()).getWeight()).equals("false")){
                           v=path.getLast().getVertex().getEdgeList().getFirst().getVertex();
                        }else
                           v=path.getLast().getVertex().getEdgeList().get(1).getVertex();
                        
                     }
                     l--;
                  }      
                 }
                  k++;
               }
            }   
               if(z==false)
                  v=v.getEdgeList().getFirst().getVertex();
         }
      }      
         //Para evitar ciclos infinitos si el grafo tiene algun ciclo por false
         if(!paths.isEmpty()){
         for(Path p1:path){
           for(Path p:paths.getLast()){
            if(((ConditionalInfo)p.getVertex().getInfo()).getCode().equals(((ConditionalInfo)p1.getVertex().getInfo()).getCode()))
               count1++;
         }
      }
   }
         if(count1==path.size()&& path.size()!=0 ){
             Path p=new Path("false",path.getLast().getVertex());
             path.remove(path.getLast());
             path.add(p);
         }
         if(!path.isEmpty()){   
         if(paths.isEmpty())
         paths.add(path);
         else{
            if(!temporalPaths.isEmpty()){
               temporalPaths.getLast().addAll(path);
                paths.add(temporalPaths.getLast());
            }
         }
      }else
         paths.add(temporalPaths.getLast());
         
         i=paths.getLast().size()-1;
         while(i>=0&&!f){
            path1=new LinkedList<Path>();
                temporalPaths=new LinkedList<LinkedList<Path>>();
            if(paths.getLast().get(i).getPath().equals("true")){
               f=true;
               
               for(int j=0;j<=i;j++){
                  path1.add(paths.getLast().get(j));
               }
                Path p=new Path("false",path1.getLast().getVertex());
                path1.removeLast();
                path1.addLast(p);
            }
            if(!path1.isEmpty()){
               temporalPaths.add(path1);
            if(((String)((WeightedEdge)path1.getLast().getVertex().getEdgeList().getFirst()).getWeight()).equals("false")){
               v=path1.getLast().getVertex().getEdgeList().getFirst().getVertex();
            }else{
               v=path1.getLast().getVertex().getEdgeList().get(1).getVertex();
            }
         }
            i--;
         }
         if(count1==path.size()&& path.size()!=0 )
            paths.get(paths.size()-1).getLast().setPath("true");
         
      }while(paths.size()<count+1);
      return paths;   
   }

// Main
package main;

import java.util.LinkedList;

import directGraph.ILinkedWeightedEdgeDirectedGraph;
import utilsPackage.BinaryTree;
import utilsPackage.BinaryTreeNode;
import vertex.Vertex;
import edge.WeightedEdge;
import flowGraph.ConditionalInfo;
import flowGraph.ExpresionNode;
import flowGraph.FlowGraphManagement;
import flowGraph.Path;
import graph.LinkedGraph;

public class Main2 {
   
   
public static void main(String[] args) {
   
   // Creación de las expresiones
   BinaryTree<ExpresionNode>t1=new BinaryTree<ExpresionNode>();
   BinaryTreeNode<ExpresionNode>n1=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en=new ExpresionNode("","+","operador");
   n1.setInfo(en);
   BinaryTreeNode<ExpresionNode>n2=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en2=new ExpresionNode("","-","operador");
   n2.setInfo(en2);
   BinaryTreeNode<ExpresionNode>n3=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en3=new ExpresionNode("","*","operador");
   n3.setInfo(en3);
   BinaryTreeNode<ExpresionNode>n4=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en4=new ExpresionNode("int","47","constante");
   n4.setInfo(en4);
   BinaryTreeNode<ExpresionNode>n5=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en5=new ExpresionNode("float","y","variable");
   n5.setInfo(en5);
   BinaryTreeNode<ExpresionNode>n6=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en6=new ExpresionNode("int","z","variable");
   n6.setInfo(en6);
   BinaryTreeNode<ExpresionNode>n7=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en1=new ExpresionNode("int","3","constante");
   n7.setInfo(en1);
   n1.setLeft(n4);
   n2.setLeft(n5);
   n3.setLeft(n6);
   n3.setRight(n7);
   n2.setRight(n3);
   n1.setRight(n2);
   t1.setRoot(n1);
   
   BinaryTree<ExpresionNode>t2=new BinaryTree<ExpresionNode>();
   BinaryTreeNode<ExpresionNode>n8=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en7=new ExpresionNode("int","10","constante");
   n8.setInfo(en7);
   t2.setRoot(n8);
   
   BinaryTree<ExpresionNode>t3=new BinaryTree<ExpresionNode>();
   BinaryTreeNode<ExpresionNode>n9=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en8=new ExpresionNode("float","x","variable");
   n9.setInfo(en8);
   t3.setRoot(n9);
   
   
   BinaryTree<ExpresionNode>t4=new BinaryTree<ExpresionNode>();
   BinaryTreeNode<ExpresionNode>n10=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en9=new ExpresionNode("int","z","variable");
   n10.setInfo(en9);
   BinaryTreeNode<ExpresionNode>n11=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en10=new ExpresionNode("","-","operador");
   n11.setInfo(en10);
   BinaryTreeNode<ExpresionNode>n12=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en11=new ExpresionNode("float","y","variable");
   n12.setInfo(en11);
   n11.setLeft(n10);
   n11.setRight(n12);
   t4.setRoot(n11);
   
   
   
   BinaryTree<ExpresionNode>t5=new BinaryTree<ExpresionNode>();
   BinaryTreeNode<ExpresionNode>n13=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en12=new ExpresionNode("float","x","variable");
   n13.setInfo(en12);
   BinaryTreeNode<ExpresionNode>n14=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en13=new ExpresionNode("","+","operador");
   n14.setInfo(en13);
   BinaryTreeNode<ExpresionNode>n15=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en14=new ExpresionNode("int","10","constante");
   n15.setInfo(en14);
   n14.setLeft(n13);
   n14.setRight(n15);
   t5.setRoot(n14);
   
   BinaryTree<ExpresionNode>t6=new BinaryTree<ExpresionNode>();
   BinaryTreeNode<ExpresionNode>n16=new BinaryTreeNode<ExpresionNode>();
   ExpresionNode en15=new ExpresionNode("float","20","constante");
   n16.setInfo(en15);
   t6.setRoot(n16);
   
   // Creación de los vértices no ponderados
   Vertex v0 = new Vertex("#");
   Vertex v1 = new Vertex(new ConditionalInfo("A",t1,t2,"<"));
   Vertex v2 = new Vertex(new ConditionalInfo("B",t5,t6,"!="));
   Vertex v3 = new Vertex(new ConditionalInfo("C",t3,t4,">="));
   Vertex v4 = new Vertex("D");
   Vertex v5 = new Vertex( new ConditionalInfo("E",t5,t6,"=="));
   Vertex v6 = new Vertex("F");
   Vertex v7 = new Vertex("G");
   
   
   //Creación de los arcos ponderados de los vértices no ponderados
   WeightedEdge wE0 = new WeightedEdge(v0,"true");
   WeightedEdge wE01 = new WeightedEdge(v0,"false");
      WeightedEdge wE2 = new WeightedEdge(v2,"true");
   WeightedEdge wE3 = new WeightedEdge(v3,"false");
   WeightedEdge wE4 = new WeightedEdge(v4,"false");
   WeightedEdge wE5 = new WeightedEdge(v5,"true");
   WeightedEdge wE6 = new WeightedEdge(v6,"true");
   WeightedEdge wE1 = new WeightedEdge(v1,"false");
   WeightedEdge wE7 = new WeightedEdge(v7,"true");
   WeightedEdge wE31 = new WeightedEdge(v3,"true");
   WeightedEdge wE61 = new WeightedEdge(v6,"false");
   
   // Añadir arcos ponderados a los vértices no ponderados (dirigido)
   v1.getEdgeList().clear();
   v2.getEdgeList().clear();
   v3.getEdgeList().clear();
   v4.getEdgeList().clear();
   v5.getEdgeList().clear();
   v0.getEdgeList().add(wE1);
   v1.getEdgeList().add(wE2);
   v1.getEdgeList().add(wE3);
   v3.getEdgeList().add(wE5);
   v3.getEdgeList().add(wE4);
   v4.getEdgeList().add(wE6);
   v5.getEdgeList().add(wE3);
   v5.getEdgeList().add(wE6);
   
   //Construir grafo dirigido con vértices no ponderados y arcos ponderados
   ILinkedWeightedEdgeDirectedGraph weightedEdgeDG = new LinkedGraph();
   weightedEdgeDG.getVerticesList().add(v0);
   weightedEdgeDG.getVerticesList().add(v1);
   weightedEdgeDG.getVerticesList().add(v2);
   weightedEdgeDG.getVerticesList().add(v3);
   weightedEdgeDG.getVerticesList().add(v4);
   weightedEdgeDG.getVerticesList().add(v5);
   weightedEdgeDG.getVerticesList().add(v6);
   weightedEdgeDG.getVerticesList().add(v7);
   
   LinkedList<LinkedList<String>>e1=new LinkedList<LinkedList<String>>();
   LinkedList<String>e=null;
    LinkedList<LinkedList<Path>> independentPath=FlowGraphManagement.independentPath(weightedEdgeDG);
    for(LinkedList<Path> p:independentPath){
       e=new LinkedList<String>();
       for(Path p1: p){
          e.add(((ConditionalInfo)p1.getVertex().getInfo()).getCode()+"_"+p1.getPath());
       }
       e1.add(e);
    }
    for(LinkedList<String>s:e1)
    System.out.println(s);
   
}
   
   
}



En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Grafos
Java
soser 1 3,093 Último mensaje 4 Noviembre 2010, 22:53 pm
por Debci
Siguiendo con grafos...
Java
soser 0 1,685 Último mensaje 23 Noviembre 2010, 06:38 am
por soser
grafos
Programación General
kailon 3 3,502 Último mensaje 6 Junio 2011, 16:54 pm
por Valkyr
Imprimir Grafos en C
Programación C/C++
JorgeKun 0 11,625 Último mensaje 12 Junio 2011, 21:59 pm
por JorgeKun
¿Mejor algoritmo? Problema con aproximaciones de Grafos « 1 2 »
Programación C/C++
KINGARZA 14 6,516 Último mensaje 30 Julio 2016, 18:04 pm
por AlbertoBSD
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines