mihainan

Lab03 - POO

Oct 21st, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package Lab03;
  2.  
  3. /**
  4.  *
  5.  * @author Nan Mihai
  6.  */
  7. public class Graph {
  8.     private boolean mat[][];
  9.     private int nr;
  10.    
  11.     public Graph(int nr) {
  12.         this.nr = nr;
  13.         this.mat = new boolean[nr][nr];
  14.     }
  15.    
  16.     public int size() {
  17.         Graph g = this;
  18.         return g.nr;
  19.     }
  20.    
  21.     public void addArc(int v, int w) {
  22.         Graph g = this;
  23.         g.mat[v][w] = true;
  24.     }
  25.    
  26.     public boolean isArc(int v, int w) {
  27.         return this.mat[v][w];
  28.     }
  29.    
  30.     public void print() {
  31.         Graph g = this;
  32.         for(int i = 0; i < g.nr; i++) {
  33.             for(int j = 0; j < g.nr; j++) {
  34.                 if(g.mat[i][j] == true){
  35.                     System.out.print("1 ");
  36.                 } else {
  37.                     System.out.print("0 ");
  38.                 }
  39.             }
  40.             System.out.println();
  41.         }
  42.     }
  43.    
  44.     public void dfs(int v, boolean vazut[]) {
  45.         int w;
  46.         vazut[v] = true;
  47.         for(w = 0; w < this.nr; w++) {
  48.             if(isArc(v, w) && !vazut[w]) {
  49.                 System.out.println(v + " - " + w);
  50.                 dfs(w, vazut);
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment