Durchläufe durch Graphen SS12

Aus ProgrammingWiki
< AuK
Archivierte Version vom 2012-04-17T16:46:28+00:00. Aktuelle Version

Java-Test

class StackX
   {
   private final int SIZE = 20;
   private int[] st;
   private int top;

   public StackX()           // constructor
      {
      st = new int[SIZE];    // make array
      top = -1;
      }

   public void push(int j)   // put item on stack
      { st[++top] = j; }

   public int pop()          // take item off stack
      { return st[top--]; }

   public int peek()         // peek at top of stack
      { return st[top]; }

   public boolean isEmpty()  // true if nothing on stack
      { return (top == -1); }

   }
class Vertex
   {
   public char label;        // label (e.g. 'A')
   public boolean wasVisited;

   public Vertex(char lab)   // constructor
      {
      label = lab;
      wasVisited = false;
      }

   }

class Graph

  {
  private final int MAX_VERTS = 20;
  private Vertex vertexList[]; // list of vertices
  private int adjMat[][];      // adjacency matrix
  private int nVerts;          // current number of vertices
  private StackX theStack;
  public Graph()               // constructor
     {
     vertexList = new Vertex[MAX_VERTS];
                                         // adjacency matrix
     adjMat = new int[MAX_VERTS][MAX_VERTS];
     nVerts = 0;
     for(int y=0; y<max_verts y set adjacency for x="0;" matrix to adjmat thestack="new" stackx end constructor><p>// ------------------------------------------------------------</p><pre>  public void addVertex(char lab)
     {
     vertexList[nVerts++] = new Vertex(lab);
     }</pre><p>// ------------------------------------------------------------</p><pre>  public void addEdge(int start, int end)
     {
     adjMat[start][end] = 1;
     adjMat[end][start] = 1;
     }</pre><p>// ------------------------------------------------------------</p><pre>  public void displayVertex(int v)
     {
     System.out.print(vertexList[v].label);
     }</pre><p>// ------------------------------------------------------------</p><pre>  public void dfs()  // depth-first search
     {                                 // begin at vertex 0
     vertexList[0].wasVisited = true;  // mark it
     displayVertex(0);                 // display it
     theStack.push(0);                 // push it</pre><pre>     while( !theStack.isEmpty() )      // until stack empty,
        {
        // get an unvisited vertex adjacent to stack top
        int v = getAdjUnvisitedVertex( theStack.peek() );
        if(v == -1)                    // if no such vertex,
           theStack.pop();
        else                           // if it exists,
           {
           vertexList[v].wasVisited = true;  // mark it
           displayVertex(v);                 // display it
           theStack.push(v);                 // push it
           }
        }  // end while</pre><pre>     // stack is empty, so we're done
     for(int j=0; j<nverts j reset flags vertexlist false end dfs><p>// ------------------------------------------------------------</p><pre>  // returns an unvisited vertex adj to v
  public int getAdjUnvisitedVertex(int v)
     {
     for(int j=0; j<nverts j if vertexlist return end getadjunvisitedvertex><p>// ------------------------------------------------------------</p><pre>  }  // end class Graph</pre><p>////////////////////////////////////////////////////////////////</p>

<p>/*Stack01 = Stack(); Stack01.push(1); Stack01.push(2); Stack01.push(4); Stack01.push(5); Stack01.push(3);</p><p>VertexA = Vertex('A'); System.out.println(VertexA.wasVisited + "\n" + VertexA.label); System.out.println(Stack01.top);*/</p><p>Graph01 = Graph(); Graph01.addEdge(1,3);</p><p>for (int k = 0; k &lt; Graph01.max_verts ; ++k) {</p><pre> for (int l = 0; l &lt; Graph01.max_verts ; ++l) { System.out.print(Graph01.adjMat[k][l] + "\t");}</pre><p>System.out.println("");}</p><p></p></nverts></pre></nverts></pre></max_verts>