Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LK IF-1 J Q1
- // graphische Benutzeroberfläche zum ADT Graph.
- // Rg, 13.3.2013
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Random;
- import javax.swing.*;
- public class GUI extends JFrame implements ActionListener
- {
- private static final boolean EXIT = false;
- private JTextField tfAnzeige;
- private boolean gefunden;
- private String namen;
- private List list;
- private List weg;
- private Queue breit=new Queue();
- private String[] knoten = {"Eins", "Zwei", "Drei", "Vier", "Fünf", "Sechs", "Sieben", "Acht","Neun"} ;
- public boolean breitensuche;
- //MARTEN
- Graph graph123 = new Graph();
- GraphNode[] alleKnoten;
- Random zufall123 = new Random();
- int n;
- double[][] adjazenz;
- 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}} ;
- // 0 1 0 1
- // 1 0 0 1
- // 0 0 0 1
- // 1 1 1 0
- private Graph graph = new Graph();
- public GUI()
- {
- super.getContentPane().setLayout(null);
- super.getContentPane().setBackground(Color.lightGray);
- //Textfelder
- tfAnzeige = new JTextField (20);
- tfAnzeige.setBounds (250, 100, 150, 30);
- super.getContentPane().add (tfAnzeige);
- //Dialog-Buttons
- JButton button1 = new JButton("Test1");
- button1.setBounds (50, 30, 150, 25);
- button1.setBackground (Color.yellow);
- button1.addActionListener(this);
- this.getContentPane().add(button1);
- JButton button2 = new JButton("Suche");
- button2.setBounds (50, 60, 150, 25);
- button2.setBackground (Color.yellow);
- button2.addActionListener(this);
- this.getContentPane().add(button2);
- JButton button3 = new JButton("Test3");
- button3.setBounds (50, 90, 150, 25);
- button3.setBackground (Color.yellow);
- button3.addActionListener(this);
- this.getContentPane().add(button3);
- JButton button4 = new JButton("Test4");
- button4.setBounds (50, 120, 150, 25);
- button4.setBackground (Color.yellow);
- button4.addActionListener(this);
- this.getContentPane().add(button4);
- //Window-Listener
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent event){
- setVisible(false);
- dispose();
- System.exit(0);
- }
- });
- }
- public Graph matrixToGraph(int[][] matrix, String[] knoten)
- {
- Graph ausgabeGraph = new Graph();
- int n = matrix.length;
- for (int i=0; i<n; i++) // Einfuegen der Knoten in den Graph
- {
- GraphNode aktNode = new GraphNode(knoten[i]);
- ausgabeGraph.addNode(aktNode);
- }
- for (int i=0; i<n; i++)
- {
- GraphNode aktNode01 = ausgabeGraph.getNode(knoten[i]);
- for(int j=0; j<n; j++)
- {
- GraphNode aktNode02 = ausgabeGraph.getNode(knoten[j]);
- if(matrix[i][j]==1)
- {
- ausgabeGraph.addEdge(aktNode01, aktNode02, 100);
- }
- }
- }
- return ausgabeGraph;
- }
- public List nachbarListe(Graph graph, String knoten, String[] alleKnoten)
- {
- List ausgabeListe = new List();
- GraphNode temp = graph.getNode(knoten);
- int n = alleKnoten.length;
- for(int i=0; i<n; i++)
- {
- if(graph.hasEdge(graph.getNode(knoten), graph.getNode(alleKnoten[i])))
- {
- ausgabeListe.append(alleKnoten[i]);
- }
- }
- return ausgabeListe;
- }
- public void sucheWege(Graph graph, String startknoten, String endknoten)
- {
- GraphNode temp = new GraphNode(startknoten);
- temp.mark();
- if(temp.getNeighbours_().hasAccess())
- {
- GraphNode temp1 = (GraphNode)temp.getNeighbours_().getObject();
- if(temp1.getName()==endknoten)
- {
- gefunden = true;
- }
- else if(temp1.getName()!=endknoten)
- {
- sucheWege(graph, temp1.getName(), endknoten);
- }
- temp.getNeighbours_().next();
- }
- else if(!temp.getNeighbours_().hasAccess())
- {
- }
- }
- public void sucheWege1(Graph graph, String startknoten, String endknoten)
- {
- GraphNode temp = new GraphNode(startknoten);
- temp.mark();
- list.insert(startknoten);
- if(temp.getNeighbours_().hasAccess())
- {
- GraphNode temp1 = (GraphNode)temp.getNeighbours_().getObject();
- list.insert(temp1.getName());
- if(temp1.isMarked())
- {
- list.remove();
- temp.getNeighbours_().next();
- sucheWege1(graph, temp.getName(), endknoten) ;
- }
- else if(!temp1.isMarked())
- {
- if(temp1.getName()==endknoten)
- {
- gefunden = true;
- }
- else
- {
- temp1.mark();
- sucheWege1(graph, temp1.getName(),endknoten);
- }
- }
- }
- else if(temp.getNeighbours_().hasAccess())
- {
- list.remove();
- list.toLast();
- sucheWege(graph,(String) list.getObject(), endknoten);
- }
- }
- public void sucheWegeFinal(Graph graph, String startknoten, String endknoten)
- {
- GraphNode knoten = graph.getNode(startknoten);
- knoten.mark();
- weg.insert(knoten.getName());
- weg.toLast();
- List list1 = graph.getNode(startknoten).getNeighbours_();
- for(list1.toFirst();list1.hasAccess();list.next())
- {
- GraphNode temp = (GraphNode) list1.getObject();
- if(graph.hasEdge(temp, graph.getNode(endknoten)))
- {
- weg.append(temp.getName());
- weg.append(endknoten);
- }
- else
- {
- sucheWegeFinal(graph,temp.getName(),endknoten);
- }
- }
- }
- public void sucheWege2(Graph graph, String startknoten, String endknoten)
- {
- GraphNode temp = graph.getNode(startknoten);
- temp.mark();
- List list1 = graph.getNode(startknoten).getNeighbours_();
- list1.toLast();
- GraphNode temp2=(GraphNode)list1.getObject();
- if(temp2.isMarked())
- {
- if(temp2.getNeighbours_().hasAccess())
- {
- sucheWege2(graph, temp2.getName(), endknoten);
- }
- else
- {
- list1.remove();
- list1.toLast();
- temp2=(GraphNode)list1.getObject();
- sucheWege2(graph, temp2.getName(),endknoten);
- }
- }
- if(!temp2.isMarked())
- {
- if(temp2.getName()==endknoten)
- {
- gefunden=true;
- }
- else if(temp2.getName()!= endknoten)
- {
- temp2.mark();
- if(temp2.getNeighbours_().hasAccess())
- {
- sucheWege2(graph, temp2.getName(), endknoten);
- }
- else
- {
- list1.remove();
- list1.toLast();
- temp2=(GraphNode)list1.getObject();
- sucheWege2(graph, temp2.getName(),endknoten);
- }
- }
- }
- }
- public void searchWays(Graph graph, String startknoten, String endknoten)
- {
- GraphNode temp = graph.getNode(startknoten);
- temp.mark();
- List list1 = graph.getNode(startknoten).getNeighbours_();
- list1.toLast();
- GraphNode temp2=(GraphNode)list1.getObject();
- for(list1.toFirst(); list1.hasAccess(); list1.next())
- {
- GraphNode i = (GraphNode)list1.getObject();
- if(!i.isMarked()&&i.getName()==endknoten)
- {
- gefunden=true;
- namen=namen+" , "+i.getName();
- }
- }
- }
- public List adListe (Graph pgraph)
- {
- List temp = pgraph.getNodes();
- List temp2 = new List();
- temp2.toFirst();
- List finalList = new List();
- finalList.toFirst();
- for(temp.toFirst(); temp.hasAccess(); temp.next())
- {
- temp2.append((GraphNode)temp.getObject());
- //temp2.concat(pgraph.getNeighbours((GraphNode)temp.getObject()));
- GraphNode name = (GraphNode)temp.getObject();
- List pTemp = pgraph.getNeighbours(name);
- for(pTemp.toFirst();pTemp.hasAccess();pTemp.next())
- {
- temp2.append(pTemp.getObject());
- }
- finalList.append(temp2);
- temp2 = new List();
- temp2.toFirst();
- }
- return finalList;
- }
- public Graph listToGraph(List pList)
- {
- Graph graphFinal = new Graph();
- for(pList.toFirst();pList.hasAccess();pList.next())
- {
- List temp = (List)pList.getObject();
- temp.toFirst();
- GraphNode knoten =(GraphNode)temp.getObject();
- graphFinal.addNode(knoten);
- temp.remove();
- }
- for(pList.toFirst();pList.hasAccess();pList.next())
- {
- List tempX = (List)pList.getObject();
- tempX.toFirst();
- GraphNode temp=(GraphNode)tempX.getObject();
- tempX.remove();
- //GraphNode knoten1 = graphFinal.getNode(temp.getName());
- for(tempX.toFirst();tempX.hasAccess();tempX.next())
- {
- graphFinal.addEdge(graphFinal.getNode(temp.getName()), (GraphNode)tempX.getObject(), 0);
- }
- }
- return graphFinal;
- }
- public int[][] GraphToMatrix(Graph pGraph)
- {
- List knoten = pGraph.getNodes(); //Liste mit allen knoten
- knoten.toFirst();
- while(knoten.hasAccess())
- {
- n++; //anzahl der Knoten
- knoten.next();
- }
- GraphNode[] pKnoten = new GraphNode[n];
- knoten.toFirst();
- for(int i=0; i < n; i++)
- {
- pKnoten[i] = (GraphNode) knoten.getObject(); //knoten werden in array übertragen
- knoten.next();
- }
- int [][]pAdjazenz = new int[n][n];
- return pAdjazenz;
- }
- public boolean breitensuche2(Graph pgraph,String startknoten,String suchknoten)
- {
- breit.enqueue(pgraph.getNode(startknoten));
- pgraph.getNode(startknoten).mark();
- while(!breit.isEmpty())
- {
- GraphNode currentNode = (GraphNode)breit.front(); // Es wird eine aktuelle Node erstellt, mit der
- System.out.println(currentNode.getName()); // fortgefahren wird. Diese wird ausgegeben
- if(currentNode==pgraph.getNode(suchknoten))
- {
- return true; // Falls der Knoten der aktuellen Node entspricht,
- // wurde der gesuchte Knoten gefunden (true wird ausgegeben)
- }
- else
- {
- List temp = currentNode.getNeighbours_();
- temp.toFirst();
- while(temp.hasAccess()) // Es werden alle Nachbarn der aktuellen Node in eine Liste gepackt
- {
- GraphNode checkNode = (GraphNode)temp.getObject();
- if(!checkNode.isMarked()) //falls das Objekt der List noch nicht markiert ist kommt es in die Queue
- {
- breit.enqueue(checkNode);
- checkNode.mark();
- temp.next();
- }
- else
- {
- temp.next();
- }
- }
- currentNode.mark();
- breit.dequeue();
- }
- }
- return false;
- }
- //public boolean tiefensuche(Graph pgraph, String startknoten, String suchknoten)
- //{
- //}
- public void actionPerformed(ActionEvent event)
- {
- String cmd = event.getActionCommand();
- if (cmd.equals("Test1"))
- {
- graph = matrixToGraph(adjazenzMatrix, knoten);
- System.out.println("Graph wurde erstellt.");
- }
- else if (cmd.equals("Suche"))
- {
- String text = tfAnzeige.getText();
- if(graph.hasNode(text))
- {
- System.out.println("Der Graph besitzt einen Knoten mit Namen "+text+".");
- }
- else
- {
- System.out.println("Ein Knoten mit Namen "+text+" wurde nicht gefunden.");
- }
- }
- else if(cmd.equals("Test3"))
- {
- List temp = nachbarListe(graph, "Eins", knoten);
- List temp2 = nachbarListe(graph, "Zwei", knoten);
- List temp3 = nachbarListe(graph, "Drei", knoten);
- List temp4 = nachbarListe(graph, "Vier", knoten);
- temp.toFirst();
- temp2.toFirst();
- temp3.toFirst();
- temp4.toFirst();
- System.out.println(" "+adjazenzMatrix[0][0]+adjazenzMatrix[0][1]+adjazenzMatrix[0][2]+adjazenzMatrix[0][3]+" ");
- System.out.println(" "+adjazenzMatrix[1][0]+adjazenzMatrix[1][1]+adjazenzMatrix[1][2]+adjazenzMatrix[1][3]+" ");
- System.out.println(" "+adjazenzMatrix[2][0]+adjazenzMatrix[2][1]+adjazenzMatrix[2][2]+adjazenzMatrix[2][3]+" ");
- System.out.println(" "+adjazenzMatrix[3][0]+adjazenzMatrix[3][1]+adjazenzMatrix[3][2]+adjazenzMatrix[3][3]+" ");
- System.out.println(" ");
- System.out.println("-----------------------------");
- System.out.println("Knoten Eins");
- System.out.println("-----------------------------");
- while(temp.hasAccess())
- {
- System.out.println(" "+(String)temp.getObject());
- temp.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Zwei");
- System.out.println("-----------------------------");
- while(temp2.hasAccess())
- {
- System.out.println(" "+(String)temp2.getObject());
- temp2.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Drei");
- System.out.println("-----------------------------");
- while(temp3.hasAccess())
- {
- System.out.println(" "+(String)temp3.getObject());
- temp3.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Vier");
- System.out.println("-----------------------------");
- while(temp4.hasAccess())
- {
- System.out.println(" "+(String)temp4.getObject());
- temp4.next();
- }
- }
- else if(cmd.equals("Test4"))
- {
- /*List temp =adListe(graph);
- temp.toFirst();
- for(temp.toFirst();temp.hasAccess();temp.next())
- {
- System.out.println(temp.getObject()+"");
- List list =(List) temp.getObject();
- for(list.toFirst();list.hasAccess();list.next())
- {
- GraphNode lol = (GraphNode)list.getObject();
- System.out.println(lol.getName()+"");
- }
- }
- Graph graph1 = listToGraph(temp);
- List temp2 = nachbarListe(graph1, "Eins", knoten);
- List temp3 = nachbarListe(graph1, "Zwei", knoten);
- List temp4 = nachbarListe(graph1, "Drei", knoten);
- List temp5 = nachbarListe(graph1, "Vier", knoten);
- System.out.println("-----------------------------");
- System.out.println("Knoten Eins");
- System.out.println("-----------------------------");
- while(temp.hasAccess())
- {
- System.out.println(" "+(String)temp.getObject());
- temp.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Zwei");
- System.out.println("-----------------------------");
- while(temp2.hasAccess())
- {
- System.out.println(" "+(String)temp2.getObject());
- temp2.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Drei");
- System.out.println("-----------------------------");
- while(temp3.hasAccess())
- {
- System.out.println(" "+(String)temp3.getObject());
- temp3.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Vier");
- System.out.println("-----------------------------");
- while(temp4.hasAccess())
- {
- System.out.println(" "+(String)temp4.getObject());
- temp4.next();
- }
- List list =graph1.getNodes();
- list.toFirst();
- for(list.toFirst();list.hasAccess();list.next())
- {
- GraphNode lala=(GraphNode)list.getObject();
- System.out.println(""+lala.getName());
- }
- }
- */
- System.out.println(""+ breitensuche2(graph,"Eins","Acht")) ;
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement