Unit 10 Chapter 13 Graphs Code Flashcards
1
Q
dfs
A
//////////////////////////////////////////////////////////////// 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); } // ------------------------------------------------------------ } // end class StackX //////////////////////////////////////////////////////////////// class Vertex { public char label; // label (e.g. 'A') public boolean wasVisited; // ------------------------------------------------------------ public Vertex(char lab) // constructor { label = lab; wasVisited = false; } // ------------------------------------------------------------ } // end class Vertex //////////////////////////////////////////////////////////////// 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); theGraph.dfs(); // depth-first search System.out.println(); } // end main() } // end class DFSApp ////////////////////////////////////////////////////////////////
2
Q
bfs
A
//////////////////////////////////////////////////////////////// class Queue { private final int SIZE = 20; private int[] queArray; private int front; private int rear; // ------------------------------------------------------------- public Queue() // constructor { queArray = new int[SIZE]; front = 0; rear = -1; } // ------------------------------------------------------------- public void insert(int j) // put item at rear of queue { if(rear == SIZE-1) rear = -1; queArray[++rear] = j; } // ------------------------------------------------------------- public int remove() // take item from front of queue { int temp = queArray[front++]; if(front == SIZE) front = 0; return temp; } // ------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return ( rear+1==front || (front+SIZE-1==rear) ); } // ------------------------------------------------------------- } // end class Queue //////////////////////////////////////////////////////////////// class Vertex { public char label; // label (e.g. 'A') public boolean wasVisited; // ------------------------------------------------------------- public Vertex(char lab) // constructor { label = lab; wasVisited = false; } // ------------------------------------------------------------- } // end class Vertex //////////////////////////////////////////////////////////////// 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 Queue theQueue; // ------------------------------------------------------------ public Graph() // constructor { vertexList = new Vertex[MAX_VERTS]; // adjacency matrix adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; for(int j=0; j); theGraph.bfs(); // breadth-first search System.out.println(); } // end main() } // end class BFSApp ////////////////////////////////////////////////////////////////
3
Q
path
A
//////////////////////////////////////////////////////////////// class DistPar // distance and parent { // items stored in sPath array public int distance; // distance from start to this vertex public int parentVert; // current parent of this vertex // ------------------------------------------------------------- public DistPar(int pv, int d) // constructor { distance = d; parentVert = pv; } // ------------------------------------------------------------- } // end class DistPar /////////////////////////////////////////////////////////////// class Vertex { public char label; // label (e.g. 'A') public boolean isInTree; // ------------------------------------------------------------- public Vertex(char lab) // constructor { label = lab; isInTree = false; } // ------------------------------------------------------------- } // end class Vertex //////////////////////////////////////////////////////////////// class Graph { private final int MAX_VERTS = 20; private final int INFINITY = 1000000; private Vertex vertexList[]; // list of vertices private int adjMat[][]; // adjacency matrix private int nVerts; // current number of vertices private int nTree; // number of verts in tree private DistPar sPath[]; // array for shortest-path data private int currentVert; // current vertex private int startToCurrent; // distance to currentVert // ------------------------------------------------------------- public Graph() // constructor { vertexList = new Vertex[MAX_VERTS]; // adjacency matrix adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; nTree = 0; for(int j=0; j< nVerts) { int indexMin = getMin(); // get minimum from sPath int minDist = sPath[indexMin].distance;
if(minDist == INFINITY) // if all infinite { // or in tree, System.out.println("There are unreachable vertices"); break; // sPath is complete } else { // reset currentVert currentVert = indexMin; // to closest vert startToCurrent = sPath[indexMin].distance; // minimum distance from startTree is // to currentVert, and is startToCurrent } // put current vertex in tree vertexList[currentVert].isInTree = true; nTree++; adjust_sPath(); // update sPath[] array } // end while(nTree< sPathDist) // if shorter, { // update sPath sPath[column].parentVert = currentVert; sPath[column].distance = startToFringe; } column++; } // end while(column < nVerts) } // end adjust_sPath() // ------------------------------------------------------------- public void displayPaths() { for(int j=0; j); theGraph.path(); // shortest paths System.out.println(); } // end main() } // end class PathApp ////////////////////////////////////////////////////////////////