Advertisement
cr34tiv3

Untitled

May 2nd, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.22 KB | None | 0 0
  1. // LK IF-1 J Q1
  2. //  graphische Benutzeroberfläche zum ADT Graph.
  3.  
  4. //                                       Rg, 13.3.2013
  5.  
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.util.Random;
  9.  
  10. import javax.swing.*;
  11.  
  12. public class GUI extends JFrame implements ActionListener
  13. {  
  14.    private static final boolean EXIT = false;
  15. private JTextField tfAnzeige;
  16.    private boolean gefunden;
  17.    private String namen;
  18.    private List list;
  19.    private List weg;
  20.    private Queue breit=new Queue();
  21.    private String[] knoten = {"Eins", "Zwei", "Drei", "Vier", "Fünf", "Sechs", "Sieben", "Acht","Neun"} ;
  22.    public boolean breitensuche;
  23.    
  24.    
  25.    //MARTEN
  26.    Graph graph123 = new Graph();
  27.    GraphNode[] alleKnoten;
  28.    Random zufall123 = new Random();
  29.    int n;
  30.    double[][] adjazenz;
  31.    
  32.    private int[][] adjazenzMatrix = { {0,1,1,1,0,0,0,0,0}, {1,0,1,0,1,0,0,0,0}, {1,1,0,0,1,0,0,0,0}, {1,0,0,0,0,1,0,0,0}, {0,1,1,0,0,0,0,1,0}, {0,0,0,1,0,0,1,0,0}, {0,0,0,0,0,1,0,1,1},{0,0,0,0,1,0,1,0,1},{0,0,0,0,0,0,1,1,0}} ;
  33.  //  0 1 0 1
  34.  //  1 0 0 1
  35.  //  0 0 0 1
  36.  //  1 1 1 0
  37.    private Graph graph = new Graph();
  38.    
  39.     public GUI()
  40.     {
  41.         super.getContentPane().setLayout(null);
  42.         super.getContentPane().setBackground(Color.lightGray);
  43.         //Textfelder
  44.         tfAnzeige = new JTextField (20);
  45.         tfAnzeige.setBounds (250, 100, 150, 30);
  46.         super.getContentPane().add (tfAnzeige);
  47.  
  48.         //Dialog-Buttons
  49.         JButton button1 = new JButton("Test1");
  50.         button1.setBounds (50, 30, 150, 25);
  51.         button1.setBackground (Color.yellow);
  52.         button1.addActionListener(this);
  53.         this.getContentPane().add(button1);
  54.         JButton button2 = new JButton("Suche");
  55.         button2.setBounds (50, 60, 150, 25);
  56.         button2.setBackground (Color.yellow);
  57.         button2.addActionListener(this);
  58.         this.getContentPane().add(button2);
  59.         JButton button3 = new JButton("Test3");
  60.         button3.setBounds (50, 90, 150, 25);
  61.         button3.setBackground (Color.yellow);
  62.         button3.addActionListener(this);
  63.         this.getContentPane().add(button3);
  64.         JButton button4 = new JButton("Test4");
  65.         button4.setBounds (50, 120, 150, 25);
  66.         button4.setBackground (Color.yellow);
  67.         button4.addActionListener(this);
  68.         this.getContentPane().add(button4);
  69.  
  70.  
  71.  
  72.          //Window-Listener
  73.         addWindowListener(new WindowAdapter() {
  74.             public void windowClosing(WindowEvent event){
  75.                setVisible(false);
  76.                dispose();
  77.                System.exit(0);
  78.             }
  79.          });
  80.    }
  81.    
  82.    public Graph matrixToGraph(int[][] matrix, String[] knoten)
  83.    {
  84.      Graph ausgabeGraph = new Graph();
  85.      int n = matrix.length;
  86.      
  87.      for (int i=0; i<n; i++)         // Einfuegen der Knoten in den Graph
  88.      {
  89.        GraphNode aktNode = new GraphNode(knoten[i]);
  90.        ausgabeGraph.addNode(aktNode);
  91.      }
  92.      
  93.      for (int i=0; i<n; i++)
  94.      {
  95.        GraphNode aktNode01 = ausgabeGraph.getNode(knoten[i]);
  96.        
  97.        for(int j=0; j<n; j++)
  98.        {
  99.          GraphNode aktNode02 = ausgabeGraph.getNode(knoten[j]);
  100.          
  101.          if(matrix[i][j]==1)
  102.          {
  103.            ausgabeGraph.addEdge(aktNode01, aktNode02, 100);
  104.          }
  105.        }
  106.        
  107.      }
  108.      
  109.      
  110.      
  111.      return ausgabeGraph;
  112.    }
  113.    
  114.    public List nachbarListe(Graph graph, String knoten, String[] alleKnoten)
  115.    {
  116.      List ausgabeListe = new List();
  117.      GraphNode temp = graph.getNode(knoten);
  118.      int n = alleKnoten.length;
  119.    
  120.      for(int i=0; i<n; i++)
  121.      {
  122.          if(graph.hasEdge(graph.getNode(knoten), graph.getNode(alleKnoten[i])))
  123.                  {
  124.                     ausgabeListe.append(alleKnoten[i]);
  125.                  }
  126.      }
  127.      
  128.      return ausgabeListe;
  129.    }
  130.    
  131.    public void sucheWege(Graph graph, String startknoten, String endknoten)
  132.    {
  133.      GraphNode temp = new GraphNode(startknoten);
  134.      temp.mark();        
  135.      if(temp.getNeighbours_().hasAccess())
  136.      {
  137.         GraphNode temp1 = (GraphNode)temp.getNeighbours_().getObject();
  138.         if(temp1.getName()==endknoten)
  139.         {
  140.             gefunden = true;
  141.         }
  142.         else if(temp1.getName()!=endknoten)
  143.         {
  144.             sucheWege(graph, temp1.getName(), endknoten);
  145.         }
  146.         temp.getNeighbours_().next();
  147.     }
  148.      else if(!temp.getNeighbours_().hasAccess())
  149.      {
  150.        
  151.      }     
  152.    }
  153.    
  154.    public void sucheWege1(Graph graph, String startknoten, String endknoten)
  155.    {
  156.        GraphNode temp = new GraphNode(startknoten);
  157.        temp.mark();
  158.        list.insert(startknoten);
  159.        if(temp.getNeighbours_().hasAccess())
  160.        {
  161.            GraphNode temp1 = (GraphNode)temp.getNeighbours_().getObject();
  162.            list.insert(temp1.getName());
  163.            if(temp1.isMarked())
  164.            {
  165.                list.remove();
  166.               temp.getNeighbours_().next();
  167.               sucheWege1(graph, temp.getName(), endknoten) ;
  168.            }
  169.            else if(!temp1.isMarked())
  170.            {
  171.                if(temp1.getName()==endknoten)
  172.                {
  173.                    gefunden = true;
  174.                }
  175.                else
  176.                {
  177.                    temp1.mark();
  178.                    sucheWege1(graph, temp1.getName(),endknoten);
  179.                }
  180.            }
  181.        }
  182.        else if(temp.getNeighbours_().hasAccess())
  183.        {
  184.            list.remove();
  185.            list.toLast();          
  186.            sucheWege(graph,(String) list.getObject(), endknoten);
  187.        }
  188.    }
  189.    
  190.    public void sucheWegeFinal(Graph graph, String startknoten, String endknoten)
  191.    {
  192.        GraphNode knoten = graph.getNode(startknoten);
  193.        knoten.mark();      
  194.        weg.insert(knoten.getName());
  195.        weg.toLast();
  196.        List list1 = graph.getNode(startknoten).getNeighbours_();
  197.        for(list1.toFirst();list1.hasAccess();list.next())
  198.        {
  199.           GraphNode temp = (GraphNode) list1.getObject();
  200.           if(graph.hasEdge(temp, graph.getNode(endknoten)))
  201.           {
  202.               weg.append(temp.getName());
  203.               weg.append(endknoten);
  204.           }
  205.           else
  206.           {
  207.              
  208.               sucheWegeFinal(graph,temp.getName(),endknoten);
  209.           }
  210.        }
  211.        
  212.    }
  213.    
  214.    public void sucheWege2(Graph graph, String startknoten, String endknoten)
  215.    {
  216.        GraphNode temp = graph.getNode(startknoten);
  217.        temp.mark();
  218.        List list1 = graph.getNode(startknoten).getNeighbours_();
  219.        list1.toLast();
  220.        GraphNode temp2=(GraphNode)list1.getObject();
  221.        if(temp2.isMarked())
  222.        {
  223.            if(temp2.getNeighbours_().hasAccess())
  224.            {
  225.                sucheWege2(graph, temp2.getName(), endknoten);
  226.            }
  227.            else
  228.            {
  229.                list1.remove();
  230.                list1.toLast();
  231.                temp2=(GraphNode)list1.getObject();
  232.                sucheWege2(graph, temp2.getName(),endknoten);
  233.            }
  234.        }
  235.        if(!temp2.isMarked())
  236.        {
  237.            if(temp2.getName()==endknoten)
  238.            {
  239.                gefunden=true;
  240.            }
  241.            else if(temp2.getName()!= endknoten)
  242.            {
  243.                temp2.mark();
  244.                if(temp2.getNeighbours_().hasAccess())
  245.                {
  246.                    sucheWege2(graph, temp2.getName(), endknoten);
  247.                }
  248.                else
  249.                {
  250.                    list1.remove();
  251.                    list1.toLast();
  252.                    temp2=(GraphNode)list1.getObject();
  253.                    sucheWege2(graph, temp2.getName(),endknoten);
  254.                }
  255.            }
  256.        }
  257.        
  258.    }
  259.    
  260.   public void searchWays(Graph graph, String startknoten, String endknoten)
  261.   {
  262.        GraphNode temp = graph.getNode(startknoten);
  263.        temp.mark();
  264.        List list1 = graph.getNode(startknoten).getNeighbours_();
  265.        list1.toLast();
  266.        GraphNode temp2=(GraphNode)list1.getObject();
  267.        for(list1.toFirst(); list1.hasAccess(); list1.next())
  268.        {
  269.            GraphNode i = (GraphNode)list1.getObject();
  270.            if(!i.isMarked()&&i.getName()==endknoten)
  271.            {
  272.               gefunden=true;
  273.               namen=namen+" , "+i.getName();
  274.            }         
  275.        }
  276.        
  277.        
  278.   }
  279.  
  280.   public List adListe (Graph pgraph)
  281.   {
  282.       List temp = pgraph.getNodes();     
  283.       List temp2 = new List();   
  284.       temp2.toFirst();
  285.       List finalList = new List();
  286.       finalList.toFirst();
  287.       for(temp.toFirst(); temp.hasAccess(); temp.next())
  288.       {      
  289.          
  290.           temp2.append((GraphNode)temp.getObject());
  291.           //temp2.concat(pgraph.getNeighbours((GraphNode)temp.getObject()));
  292.           GraphNode name = (GraphNode)temp.getObject();
  293.           List pTemp = pgraph.getNeighbours(name);
  294.           for(pTemp.toFirst();pTemp.hasAccess();pTemp.next())
  295.           {
  296.               temp2.append(pTemp.getObject());
  297.           }
  298.           finalList.append(temp2);   
  299.           temp2 = new List();
  300.          
  301.           temp2.toFirst();
  302.       }
  303.      
  304.      return finalList;
  305.   }
  306.  
  307.   public Graph listToGraph(List pList)
  308.   {
  309.       Graph graphFinal = new Graph();
  310.       for(pList.toFirst();pList.hasAccess();pList.next())
  311.       {
  312.           List temp = (List)pList.getObject(); 
  313.           temp.toFirst();
  314.           GraphNode knoten =(GraphNode)temp.getObject();
  315.           graphFinal.addNode(knoten);
  316.           temp.remove();
  317.          
  318.       }
  319.       for(pList.toFirst();pList.hasAccess();pList.next())
  320.       {
  321.           List tempX = (List)pList.getObject();
  322.           tempX.toFirst();
  323.           GraphNode temp=(GraphNode)tempX.getObject();
  324.           tempX.remove();
  325.           //GraphNode knoten1 = graphFinal.getNode(temp.getName());
  326.          
  327.           for(tempX.toFirst();tempX.hasAccess();tempX.next())
  328.           {
  329.               graphFinal.addEdge(graphFinal.getNode(temp.getName()), (GraphNode)tempX.getObject(), 0);
  330.           }
  331.          
  332.       }
  333.       return graphFinal;
  334.   }
  335.    
  336.   public int[][] GraphToMatrix(Graph pGraph)
  337.  
  338.   {
  339.     List knoten = pGraph.getNodes();          //Liste mit allen knoten
  340.     knoten.toFirst();
  341.     while(knoten.hasAccess())
  342.     {
  343.       n++;                      //anzahl der Knoten
  344.       knoten.next();
  345.     }
  346.     GraphNode[] pKnoten = new GraphNode[n];
  347.     knoten.toFirst();
  348.     for(int i=0; i < n; i++)
  349.     {
  350.       pKnoten[i] = (GraphNode) knoten.getObject();  //knoten werden in array übertragen
  351.       knoten.next();
  352.     }
  353.     int [][]pAdjazenz = new int[n][n];    
  354.    
  355.     return pAdjazenz;
  356.   }
  357.   public boolean breitensuche2(Graph pgraph,String startknoten,String suchknoten)
  358.   {
  359.      
  360.       breit.enqueue(pgraph.getNode(startknoten));    
  361.       pgraph.getNode(startknoten).mark();
  362.         while(!breit.isEmpty())
  363.         {
  364.             GraphNode currentNode = (GraphNode)breit.front();           // Es wird eine aktuelle Node erstellt, mit der
  365.             System.out.println(currentNode.getName());                  // fortgefahren wird. Diese wird ausgegeben
  366.                 if(currentNode==pgraph.getNode(suchknoten))
  367.                 {
  368.                     return true;                                        // Falls der Knoten der aktuellen Node entspricht,
  369.                                                                         // wurde der gesuchte Knoten gefunden (true wird ausgegeben)
  370.                 }
  371.                 else
  372.                 {                  
  373.                     List temp = currentNode.getNeighbours_();
  374.                     temp.toFirst();
  375.                     while(temp.hasAccess())                             // Es werden alle Nachbarn der aktuellen Node in eine Liste gepackt
  376.                     {
  377.                         GraphNode checkNode = (GraphNode)temp.getObject();
  378.                        
  379.                         if(!checkNode.isMarked())                       //falls das Objekt der List noch nicht markiert ist kommt es in die Queue
  380.                         {
  381.                             breit.enqueue(checkNode);
  382.                             checkNode.mark();
  383.                             temp.next();
  384.                         }
  385.                         else
  386.                         {
  387.                             temp.next();
  388.                         }                      
  389.                        
  390.                     }
  391.                     currentNode.mark();
  392.                     breit.dequeue();
  393.                 }          
  394.         }
  395.         return false;
  396.      
  397.   }
  398.  
  399.  
  400.  
  401.   //public boolean tiefensuche(Graph pgraph, String startknoten, String suchknoten)  
  402.   //{
  403.      
  404.   //}
  405. public void actionPerformed(ActionEvent event)
  406.    {    
  407.         String cmd = event.getActionCommand();
  408.         if (cmd.equals("Test1"))
  409.         {
  410.           graph = matrixToGraph(adjazenzMatrix, knoten);
  411.          
  412.           System.out.println("Graph wurde erstellt.");
  413.         }
  414.         else if (cmd.equals("Suche"))
  415.         {
  416.           String text = tfAnzeige.getText();
  417.          
  418.           if(graph.hasNode(text))
  419.           {
  420.             System.out.println("Der Graph besitzt einen Knoten mit Namen "+text+".");
  421.           }
  422.           else
  423.           {
  424.             System.out.println("Ein Knoten mit Namen "+text+" wurde nicht gefunden.");
  425.           }
  426.  
  427.          
  428.         }
  429.         else if(cmd.equals("Test3"))
  430.         {
  431.             List temp = nachbarListe(graph, "Eins", knoten);
  432.             List temp2 = nachbarListe(graph, "Zwei", knoten);
  433.             List temp3 = nachbarListe(graph, "Drei", knoten);
  434.             List temp4 = nachbarListe(graph, "Vier", knoten);
  435.             temp.toFirst();
  436.             temp2.toFirst();
  437.             temp3.toFirst();
  438.             temp4.toFirst();
  439.             System.out.println("  "+adjazenzMatrix[0][0]+adjazenzMatrix[0][1]+adjazenzMatrix[0][2]+adjazenzMatrix[0][3]+"  ");
  440.             System.out.println("  "+adjazenzMatrix[1][0]+adjazenzMatrix[1][1]+adjazenzMatrix[1][2]+adjazenzMatrix[1][3]+"  ");
  441.             System.out.println("  "+adjazenzMatrix[2][0]+adjazenzMatrix[2][1]+adjazenzMatrix[2][2]+adjazenzMatrix[2][3]+"  ");
  442.             System.out.println("  "+adjazenzMatrix[3][0]+adjazenzMatrix[3][1]+adjazenzMatrix[3][2]+adjazenzMatrix[3][3]+"  ");
  443.             System.out.println(" ");
  444.             System.out.println("-----------------------------");
  445.             System.out.println("Knoten Eins");
  446.             System.out.println("-----------------------------");
  447.             while(temp.hasAccess())
  448.             {
  449.                 System.out.println(" "+(String)temp.getObject());
  450.                 temp.next();
  451.             }
  452.             System.out.println("-----------------------------");
  453.             System.out.println("Knoten Zwei");
  454.             System.out.println("-----------------------------");
  455.             while(temp2.hasAccess())
  456.             {
  457.                 System.out.println(" "+(String)temp2.getObject());
  458.                 temp2.next();
  459.             }
  460.             System.out.println("-----------------------------");
  461.             System.out.println("Knoten Drei");
  462.             System.out.println("-----------------------------");
  463.             while(temp3.hasAccess())
  464.             {
  465.                 System.out.println(" "+(String)temp3.getObject());
  466.                 temp3.next();
  467.             }
  468.             System.out.println("-----------------------------");
  469.             System.out.println("Knoten Vier");
  470.             System.out.println("-----------------------------");
  471.             while(temp4.hasAccess())
  472.             {
  473.                 System.out.println(" "+(String)temp4.getObject());
  474.                 temp4.next();
  475.             }
  476.              
  477.         }
  478.         else if(cmd.equals("Test4"))
  479.         {
  480.            
  481.             /*List temp =adListe(graph);
  482.                     temp.toFirst();
  483.             for(temp.toFirst();temp.hasAccess();temp.next())
  484.             {
  485.                 System.out.println(temp.getObject()+"");   
  486.                 List list =(List) temp.getObject();
  487.                 for(list.toFirst();list.hasAccess();list.next())
  488.                 {
  489.                     GraphNode lol = (GraphNode)list.getObject();
  490.                     System.out.println(lol.getName()+"");
  491.                 }
  492.             }
  493.            
  494.             Graph graph1 = listToGraph(temp);
  495.             List temp2 = nachbarListe(graph1, "Eins", knoten);
  496.             List temp3 = nachbarListe(graph1, "Zwei", knoten);
  497.             List temp4 = nachbarListe(graph1, "Drei", knoten);
  498.             List temp5 = nachbarListe(graph1, "Vier", knoten);
  499.            
  500.            
  501.             System.out.println("-----------------------------");
  502.             System.out.println("Knoten Eins");
  503.             System.out.println("-----------------------------");
  504.             while(temp.hasAccess())
  505.             {
  506.                 System.out.println(" "+(String)temp.getObject());
  507.                 temp.next();
  508.             }
  509.             System.out.println("-----------------------------");
  510.             System.out.println("Knoten Zwei");
  511.             System.out.println("-----------------------------");
  512.             while(temp2.hasAccess())
  513.             {
  514.                 System.out.println(" "+(String)temp2.getObject());
  515.                 temp2.next();
  516.             }
  517.             System.out.println("-----------------------------");
  518.             System.out.println("Knoten Drei");
  519.             System.out.println("-----------------------------");
  520.             while(temp3.hasAccess())
  521.             {
  522.                 System.out.println(" "+(String)temp3.getObject());
  523.                 temp3.next();
  524.             }
  525.             System.out.println("-----------------------------");
  526.             System.out.println("Knoten Vier");
  527.             System.out.println("-----------------------------");
  528.             while(temp4.hasAccess())
  529.             {
  530.                 System.out.println(" "+(String)temp4.getObject());
  531.                 temp4.next();
  532.            
  533.             }
  534.         List list =graph1.getNodes();
  535.         list.toFirst();
  536.         for(list.toFirst();list.hasAccess();list.next())
  537.         {
  538.             GraphNode lala=(GraphNode)list.getObject();
  539.              System.out.println(""+lala.getName());
  540.         }
  541.        
  542.         }
  543.         */
  544.      System.out.println(""+ breitensuche2(graph,"Eins","Acht")) ;
  545.     }
  546.    
  547.  
  548.    
  549.  
  550.  
  551. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement