AuK/Durchläufe durch Graphen SS12
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<language>Java</language>
=Java-Test=
<pre>
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); }
}
</pre>
<pre>
class Vertex
{
public char label; // label (e.g. 'A')
public boolean wasVisited;
public Vertex(char lab) // constructor
{
label = lab;
wasVisited = false;
}
}
</pre>
<pre>
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(int x=0; x<MAX_VERTS; x++) // matrix to 0
adjMat[x][y] = 0;
theStack = new StackX();
} // end constructor
// ------------------------------------------------------------
public void addVertex(char lab)
{
vertexList[nVerts++] = new Vertex(lab);
}
// ------------------------------------------------------------
public void addEdge(int start, int end)
{
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
// ------------------------------------------------------------
public void displayVertex(int v)
{
System.out.print(vertexList[v].label);
}
// ------------------------------------------------------------
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
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
// stack is empty, so we're done
for(int j=0; j<nVerts; j++) // reset flags
vertexList[j].wasVisited = false;
} // end dfs
// ------------------------------------------------------------
// returns an unvisited vertex adj to v
public int getAdjUnvisitedVertex(int v)
{
for(int j=0; j<nVerts; j++)
if(adjMat[v][j]==1 && vertexList[j].wasVisited==false)
return j;
return -1;
} // end getAdjUnvisitedVertex()
// ------------------------------------------------------------
} // end class Graph
////////////////////////////////////////////////////////////////
</pre>
<code id="4f8d8fe214252">
public Object Stack ()
{
int size = 10;
int[] st = new int[size];
int top = -1;
public void push(int j)
{st[++top] = j;}
public int pop()
{return st[top--];}
public int peek()
{return st[top];}
public boolean isEmpty()
{return (top == -1);}
return this;
}
public Object Vertex (char label)
{
char label = label;
boolean wasVisited = false;
return this;
}
public Object Graph()
{
int max_verts = 20;
int nVerts = 0;
int[][] adjMat = new int[max_verts][max_verts];
for(int i =0; i<max_verts; i++)
for(int j=0; j<max_verts; j++)
adjMat[i][j] = 0;
void addEdge(int start, int end)
{
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
//Object[] vertexList =
return this;
}
</code>
<run id="4f8d9ec24fd44">
/*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);*/
Graph01 = Graph();
Graph01.addEdge(1,3);
for (int k = 0; k < Graph01.max_verts ; ++k) {
for (int l = 0; l < Graph01.max_verts ; ++l) {
System.out.print(Graph01.adjMat[k][l] + "\t");}
System.out.println("");}
</run>