Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 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.  
  7. package Laboratoria6;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.Queue;
  13.  
  14. /**
  15.  *
  16.  * @author Mateusz Szczypek
  17.  */
  18. public class BFSGraph extends AGraph{
  19.  
  20.     public BFSGraph(int vertexCount) {
  21.         super(vertexCount);
  22.     }
  23.  
  24.    @Override
  25.     public void writeMatrix() {
  26.         System.out.print("\t");
  27.        
  28.         System.out.print("\n\n");
  29.        
  30.         for (int a=0;a<getSize();a++){
  31.             System.out.println();
  32.             for (int b=0;b<getSize();b++){
  33.                 System.out.print(matrix[a][b]+" ");
  34.             }
  35.         }
  36.         System.out.print("\n\n\n");
  37.     }
  38.  
  39.     @Override
  40.     public boolean check(int i, int j) throws IllegalArgumentException {
  41.         if (matrix[i][j]==1){
  42.             return true;
  43.         } else return false;
  44.     }
  45.  
  46.     @Override
  47.     public void connect(int i, int j) throws IllegalArgumentException {
  48.                 matrix[i][j]=1;
  49.     }
  50.  
  51.     @Override
  52.     public void writeList() {
  53.         for (int a=0;a<getSize();a++){
  54.             for (int b=0;b<getSize();b++){
  55.                 if (matrix[a][b]==1){
  56.                     System.out.print(a+1+" "+(b+1)+"\n");
  57.                 }
  58.             }
  59.             System.out.print("\n");
  60.         }
  61.     }
  62.  
  63.     @Override
  64.     public void bfs(int start) throws IllegalArgumentException {
  65.         List<Integer> q = new ArrayList<>();
  66.         boolean[] odwiedzony = new boolean[getSize()];
  67.         for (int i = 0; i < getSize(); i++) {
  68.             odwiedzony[i] = false;
  69.         }
  70.         odwiedzony[start] = true;
  71.         q.add(start);
  72.         System.out.print((start+1)+", ");
  73.         while (q.isEmpty() == false) {
  74.             int v = q.remove(0);
  75.             for (int j = 0; j < getSize(); j++) {
  76.                 if (check(v, j)==true) {
  77.                     if (odwiedzony[j] == false) {
  78.                         odwiedzony[j] = true;
  79.                         System.out.print((j+1) + ", ");
  80.                         q.add(j);
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.         System.out.println();
  86.     }
  87.    
  88.     public void odleglosc(int start) {
  89.         Queue<Integer> q = new LinkedList<>();
  90.         boolean[] odwiedzony = new boolean[getSize()];
  91.         int[] odleglosc = new int[getSize()];
  92.         for (int i = 0; i < getSize(); i++) {
  93.             odwiedzony[i] = false;
  94.             odleglosc[i] = 0;
  95.         }
  96.         odwiedzony[start] = true;
  97.         q.add(start);
  98.         while (q.isEmpty() == false) {
  99.             int v = q.remove();
  100.             for (int j = 0; j < getSize(); j++) {
  101.                 if (check(v, j)) {
  102.                     if (odwiedzony[j] == false) {
  103.                         odwiedzony[j] = true;
  104.                         odleglosc[j] = odleglosc[v] + 1;
  105.                         q.add(j);
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         System.out.println();
  111.         for (int i = 0; i < getSize(); i++) {
  112.             System.out.println("Od: " + start + " do: " + i + " Najkrotsza droga = " + odleglosc[i]);
  113.         }
  114.         System.out.println();
  115.     }
  116.    
  117.     public void odleglosc(int start, int end) {
  118.         Queue<Integer> q = new LinkedList<>();
  119.         boolean[] odwiedzony = new boolean[getSize()];
  120.         String[] droga = new String[getSize()];
  121.         for (int i = 0; i < getSize(); i++) {
  122.             odwiedzony[i] = false;
  123.             droga[i] = "";
  124.         }
  125.         odwiedzony[start] = true;
  126.         q.add(start);
  127.         while (q.isEmpty() == false) {
  128.             int v = q.remove();
  129.             for (int j = 0; j < getSize(); j++) {
  130.                 if (check(v, j)) {
  131.                     if (odwiedzony[j] == false) {
  132.                         odwiedzony[j] = true;
  133.                         droga[j] = droga[v] + j + " ";
  134.                         q.add(j);
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.         System.out.print("#Najkrotsza droga z: " + start + " do: " + end + ": " + droga[end]);
  140.         System.out.println();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement