Zum Inhalt
Durchläufe durch Graphen SS12
Aus ProgrammingWiki
<
AuK
Archivierte Version vom 2012-04-25T21:55:15+00:00.
Aktuelle Version
Java-Test
public Object Stack () //queue folgt! { int groesse = 10; int[] elemente = new int[groesse]; int top = -1; public void push(int j) {elemente[++top] = j;} public int pop() {return elemente[top--];} public int peek() {return elemente[top];} public boolean istLeer() {return (top == -1);} return this; } //works!
public Object Vertex (char label) { char label = label; boolean wasVisited = false; return this; } //should work!
//y u no accepting classes? D: public Object Graph() { int knotenanzahl = 7; Object[] knotenliste = new Object[knotenanzahl]; int knoten = 0; int[][] adjazenzMatrix = new int[knotenanzahl][knotenanzahl]; Object stacky = Stack(); /*for(int col=0; col<max_verts+1; col++) adjMat[col][0] = col; for(int row=0; row<max_verts+1; row++) adjMat[0][row] = row;*/ for(int i =0; i<knotenanzahl; i++) for(int j=0; j<knotenanzahl; j++) adjazenzMatrix[i][j] = 0; void addEdge(int start, int end) { adjazenzMatrix[start][end] = 1; //adjMat[end][start] = 1; //nur für ungerichtete Graphen!! } void addVertex(char lab) { knotenliste[knoten++] = Vertex(lab); } void displayVertex(int v) { System.out.print(knotenliste[v].label); } void dfs() { knotenliste[0].wasVisited=true; displayVertex(0); stacky.push(0); while (!stacky.istLeer()) { int v = getAdjUnvisitedVertex(stacky.peek()); if(v == -1) stacky.pop(); else { knotenliste[v].wasVisited = true; displayVertex(v); stacky.push(v); } } for(int j=0; j<knoten; j++) knotenliste[j].wasVisited = false; } int getAdjUnvisitedVertex(int v) { for(int j=0; j<nVerts; j++) if(adjazenzMatrix[v][j] == 1 && knotenliste[j].wasVisited==false) return j; return -1; } return this; }
/*Stack01 = Stack(); Stack01.push(1); Stack01.push(2); Stack01.push(4); Stack01.push(5); Stack01.push(3); VertexA = Vertex('A'); System.out.println(VertexA.wasVisited + "\n" + VertexA.label); System.out.println(Stack01.top);*/ //G1 von S.145 theGraph = Graph(); theGraph.addVertex('1'); // 0 (start for dfs) theGraph.addVertex('2'); // 1 theGraph.addVertex('3'); // 2 theGraph.addVertex('4'); // 3 theGraph.addVertex('5'); // 4 theGraph.addEdge(0, 1); // 1-2 theGraph.addEdge(1, 0); // 2-1 theGraph.addEdge(1, 3); // 2-4 theGraph.addEdge(0, 3); // 1-4 theGraph.addEdge(3, 4); // 4-5 theGraph.addEdge(4, 2); // 5-3 theGraph.addEdge(2, 5); // 3-5 theGraph.addEdge(2, 2); // 3-3 theGraph.dfs(); System.out.println(" "); for (int k = 0; k < theGraph.max_verts+1 ; ++k) { for (int l = 0; l < theGraph.max_verts+1 ; ++l) { System.out.print(theGraph.adjMat[k][l] + "\t");} System.out.println("");}
ausführen
Seite
Quelltext
Versionen/Autoren
Konto registrieren
Anmelden
Navigation
Startseite
Letzte Änderungen
Alle Seiten
Einführungskurs
Hilfe
Dateien
Suche
Suchen
Werkzeuge
Links auf diese Seite
Spezialseiten