Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // shortest path
- public class GraphAdjacentMatrix_Dijkstra_BFS {
- public static int empty = -1;
- private int adjMatrix[][];
- private int numVertices;
- private boolean visited[];
- // private int distance[];
- public GraphAdjacentMatrix_Dijkstra_BFS(int numVertices) {
- this.numVertices = numVertices;
- adjMatrix = new int[numVertices][numVertices];
- for (int i = 0; i < adjMatrix.length; i++) {
- for (int j = 0; j < adjMatrix[i].length; j++) {
- adjMatrix[i][j] = empty;
- }
- }
- visited = new boolean[numVertices];
- // distance = new int[numVertices];
- // for (int i = 0; i < distance.length; i++) {
- // distance[i] = Integer.MAX_VALUE;
- // }
- }
- public int getWeight(int i, int j){
- return adjMatrix[i][j];
- }
- public void setEdge(int i, int j, int weight) {
- adjMatrix[i][j] = weight;
- // adjMatrix[j][i] = true; // directed
- }
- public void delEdge(int i, int j) {
- adjMatrix[i][j] = empty;
- // adjMatrix[j][i] = false; // directed
- }
- public boolean isEdge(int i, int j) {
- return (adjMatrix[i][j] != -1);
- }
- public int first(int v1){
- int[] ary = this.adjMatrix[v1];
- int firstNeighborIndex = this.numVertices; // return n if none
- for (int i = 0; i < ary.length; i++) {
- if (ary[i] != empty){
- firstNeighborIndex = i;
- break;
- }
- }
- return firstNeighborIndex;
- }
- public int next(int v1, int v2){
- int[] ary = this.adjMatrix[v1];
- int nextNeighborIndex = this.numVertices; // return n if none
- for (int i = v2 + 1; i < ary.length; i++) {
- if (ary[i] != empty){
- nextNeighborIndex = i;
- break;
- }
- }
- return nextNeighborIndex; // return n if none
- }
- // calculate the shortest distances between the startingVertice to all other vertices
- public int[] dijkstra(int startingVertice) {
- // initialize the final result array
- int[] distance = new int[numVertices];
- for (int i = 0; i < distance.length; i++) {
- distance[i] = Integer.MAX_VALUE;
- }
- // the first vertex reached
- distance[startingVertice] = 0; // initialize first data - from startingVertice to each vertice (we will update this whenver we reach a vertex)
- for (int i = 0; i < this.numVertices; i++) { // process all vertices
- int v = minVertex(distance); // get the next vertice with the shorted path here
- if (v == empty) return distance;// all vertices are visited
- if (distance[v] == Integer.MAX_VALUE) return distance; // if the next vertex is not reachable
- this.visited[v] = true;
- // to think about whether it would be shorter if we take advantage the newly oppcupied vertex.
- for (int neighborV = first(v); neighborV < numVertices; neighborV = next(v, neighborV)) {
- if (distance[neighborV] > (distance[v] + getWeight(v, neighborV))) {
- distance[neighborV] = (distance[v] + getWeight(v, neighborV));
- }
- }
- }
- return distance;
- }
- // Imagine we convert the length n into n vertices and use BFS;
- // we will be making many redundant waves until we reach a vertex
- // Therefore, this method here is to help us save those steps and find the next vertex right away
- // (we could use minHeap to replace this method)
- //
- // find the shorted distance from startingVertice to D so far; this will be our next starting point
- private int minVertex(int[] distance) {
- int nextVWithShortestDistance = empty; // if all vertices are visited
- // initialize v to some unvisited vertex
- int i;
- for (i = 0; i < this.numVertices; i++) {
- if (!visited[i]) {
- nextVWithShortestDistance = i;
- break;
- }
- }
- // find the smallest D value
- for (i++; i < this.numVertices; i++) {
- if (!visited[i] && distance[i] < distance[nextVWithShortestDistance]) {
- nextVWithShortestDistance = i;
- }
- }
- return nextVWithShortestDistance;
- }
- private void Previsit(boolean[][] graph, int v) {
- System.out.println("Previsit: " + v);
- }
- private void Postvisit(boolean[][] graph, int v) {
- System.out.println("Postvisit: " + v);
- }
- public String toString() {
- StringBuilder s = new StringBuilder();
- for (int i = 0; i < numVertices; i++) {
- s.append(i + ": ");
- for (int j : adjMatrix[i]) {
- s.append((i != empty?1:0) + " ");
- }
- s.append("\n");
- }
- return s.toString();
- }
- public static void main(String[] args) {
- GraphAdjacentMatrix_Dijkstra_BFS gam = new GraphAdjacentMatrix_Dijkstra_BFS(8);
- // directed graph
- gam.setEdge(1, 2, 10);
- gam.setEdge(1, 3, 3);
- gam.setEdge(1, 4, 20);
- gam.setEdge(2, 4, 5);
- gam.setEdge(3, 2, 2);
- gam.setEdge(3, 5, 15);
- gam.setEdge(4, 5, 11);
- int[] distances = gam.dijkstra(1);
- printArray(distances);
- System.out.printf("the shortest distance between %d and %d = %d %n", 1, 4, distances[4]);
- System.out.printf("the shortest distance between %d and %d = %d %n", 1, 5, distances[5]);
- System.out.println();
- }
- ////////// UTIL ///////////
- private static void printArray(int[] ary){
- for (int i = 0; i < ary.length; i++) {
- System.out.printf("%4d" , ary[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement