Advertisement
kamasazi99

aiz 6 lab

Nov 17th, 2019
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package tgraph2;
  7.  
  8. /**
  9.  *
  10.  * @author Hubert
  11.  */
  12. public class Tgraph2 extends MGraph{
  13.  
  14.     /**
  15.      * @param args the command line arguments
  16.      */
  17.     public static void main(String[] args) {
  18.       Tgraph2 t=new Tgraph2(11);
  19.       t.addEdge(1, 2); t.addEdge(1,5); t.addEdge(1, 9);
  20.       t.addEdge(3,4);
  21.       t.addEdge(2, 3); t.addEdge(2, 4);
  22.       t.addEdge(5, 6); t.addEdge(5, 7); t.addEdge(5, 8);
  23.       t.addEdge(9, 10); t.addEdge(10, 8);
  24.       System.out.println("Ilosc krawedzi: "+t.getEdgeCount());
  25.       t.writeMatrix();
  26.     }
  27.  
  28.     public Tgraph2(int k) {
  29.         super(k);
  30.     }
  31.  
  32.     @Override
  33.     public int getEdgeCount() {
  34.         int count=0;
  35.     for(int i=0;i<getVertexCount();i++)
  36.         for(int j=0;j<getVertexCount();j++)
  37.             if(tab[i][j]==true)
  38.                 count++;
  39.     return count;
  40.     }
  41.  
  42.     @Override
  43.     public void writeMatrix() {
  44.             for(int i=0;i<getVertexCount();i++){
  45.                 System.out.println("Dla "+i+": ");
  46.                 for(int j=0;j<getVertexCount();j++){
  47.                     if(isEdge(i,j)!=false)
  48.                     System.out.print(j+" ");
  49.                    
  50.                 }
  51.                 System.out.println();
  52.             }
  53.                
  54.    
  55.     }
  56.    
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. /*
  70.  * To change this license header, choose License Headers in Project Properties.
  71.  * To change this template file, choose Tools | Templates
  72.  * and open the template in the editor.
  73.  */
  74. package tgraph2;
  75.  
  76. import java.util.ArrayList;
  77.  
  78. /**
  79.  *
  80.  * @author Hubert
  81.  */
  82. public class Bfs extends Tgraph2 implements IBfsSearchable{
  83.  
  84. boolean [] tabOdwiedzone;
  85. ArrayList kolejka = new ArrayList<>();
  86.     public static void main(String[] args) {
  87.        Bfs b=new Bfs(11);
  88.      
  89.       b.addEdge(1, 2); b.addEdge(1,5); b.addEdge(1, 9);
  90.       b.addEdge(3,4);
  91.       b.addEdge(2, 3); b.addEdge(2, 4);
  92.       b.addEdge(5, 6); b.addEdge(5, 7); b.addEdge(5, 8);
  93.       b.addEdge(9, 10); b.addEdge(10, 8);
  94.        b.bfs(1);
  95.     }
  96.  
  97.     public Bfs(int k) {
  98.         super(k);
  99.         tabOdwiedzone=new boolean[k];
  100.     }
  101.  
  102.     @Override
  103.     public void bfs(int sourceVertex) {
  104.         tabOdwiedzone[sourceVertex]=true;
  105.         kolejka.add(sourceVertex);
  106.         int vertex=0;
  107.         while(!kolejka.isEmpty())
  108.         {
  109.         vertex=(int) kolejka.get(0);
  110.         kolejka.remove(0);
  111.         odwiedz(vertex);
  112.                 for(int i=0;i<getEdgeCount();i++)
  113.                     if(tab[vertex][i]==true)
  114.                     {
  115.                             if(tabOdwiedzone[i]==false && kolejka.contains(i)==false)
  116.                         kolejka.add(i);
  117.                     }
  118.         }
  119.        
  120.     }
  121.     void odwiedz(int a){
  122.    
  123.     System.out.println("Odwiedzono :"+a+" ");
  124.     tabOdwiedzone[a]=true;
  125.     }
  126.    
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement