Advertisement
davegimo

templateGrafo

Feb 4th, 2020
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Grafo {
  4.  
  5.   public ArrayList<Nodo> lista;
  6.   public int[][] matAd;
  7.  
  8.  
  9.   public Grafo(ArrayList<Nodo> l) {
  10.  
  11.     lista = l;
  12.     matAd = new int[l.size()][l.size()];
  13.   }
  14.  
  15.   public void setAdiacenza(Nodo a, Nodo b) {
  16.  
  17.     int i = lista.indexOf(a);
  18.     int j = lista.indexOf(b);
  19.  
  20.     matAd[i][j] = 1;
  21.     matAd[j][i] = 1;
  22.  
  23.   }
  24.  
  25.  
  26.  
  27. // METTERE nel file Nodo.java il seguente codice commentato
  28.  
  29. // public class Nodo {
  30.  
  31. //   public String info;
  32.  
  33. //   public Nodo(String i) {
  34. //     info = i;
  35.  
  36. //   }
  37.  
  38. // }
  39.  
  40.   //FINE Codice da copiare
  41.  
  42.  
  43.  
  44. //Main di prova
  45.   public static void main(String[] args) {
  46.  
  47.     Nodo x = new Nodo("x");
  48.     Nodo y = new Nodo("y");
  49.     Nodo z = new Nodo("z");
  50.  
  51.     ArrayList<Nodo> l = new ArrayList<Nodo>();
  52.     l.add(x);
  53.     l.add(y);
  54.     l.add(z);
  55.  
  56.    
  57.  
  58.     Grafo g = new Grafo(l);
  59.  
  60.     g.setAdiacenza(x, y);
  61.     g.setAdiacenza(x, z);
  62.  
  63.     System.out.println();
  64.    
  65.  
  66.     // Stampa la matrice di adiacenza!
  67.     for (int i = 0; i < g.matAd.length; i++) {
  68.       for (int j = 0; j < g.matAd[i].length; j++) {
  69.         System.out.print(g.matAd[i][j]);
  70.       }
  71.       System.out.println();
  72.     }
  73.     System.out.println();
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement