Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tgraph2;
- /**
- *
- * @author Hubert
- */
- public class Tgraph2 extends MGraph{
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Tgraph2 t=new Tgraph2(11);
- t.addEdge(1, 2); t.addEdge(1,5); t.addEdge(1, 9);
- t.addEdge(3,4);
- t.addEdge(2, 3); t.addEdge(2, 4);
- t.addEdge(5, 6); t.addEdge(5, 7); t.addEdge(5, 8);
- t.addEdge(9, 10); t.addEdge(10, 8);
- System.out.println("Ilosc krawedzi: "+t.getEdgeCount());
- t.writeMatrix();
- }
- public Tgraph2(int k) {
- super(k);
- }
- @Override
- public int getEdgeCount() {
- int count=0;
- for(int i=0;i<getVertexCount();i++)
- for(int j=0;j<getVertexCount();j++)
- if(tab[i][j]==true)
- count++;
- return count;
- }
- @Override
- public void writeMatrix() {
- for(int i=0;i<getVertexCount();i++){
- System.out.println("Dla "+i+": ");
- for(int j=0;j<getVertexCount();j++){
- if(isEdge(i,j)!=false)
- System.out.print(j+" ");
- }
- System.out.println();
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tgraph2;
- import java.util.ArrayList;
- /**
- *
- * @author Hubert
- */
- public class Bfs extends Tgraph2 implements IBfsSearchable{
- boolean [] tabOdwiedzone;
- ArrayList kolejka = new ArrayList<>();
- public static void main(String[] args) {
- Bfs b=new Bfs(11);
- b.addEdge(1, 2); b.addEdge(1,5); b.addEdge(1, 9);
- b.addEdge(3,4);
- b.addEdge(2, 3); b.addEdge(2, 4);
- b.addEdge(5, 6); b.addEdge(5, 7); b.addEdge(5, 8);
- b.addEdge(9, 10); b.addEdge(10, 8);
- b.bfs(1);
- }
- public Bfs(int k) {
- super(k);
- tabOdwiedzone=new boolean[k];
- }
- @Override
- public void bfs(int sourceVertex) {
- tabOdwiedzone[sourceVertex]=true;
- kolejka.add(sourceVertex);
- int vertex=0;
- while(!kolejka.isEmpty())
- {
- vertex=(int) kolejka.get(0);
- kolejka.remove(0);
- odwiedz(vertex);
- for(int i=0;i<getEdgeCount();i++)
- if(tab[vertex][i]==true)
- {
- if(tabOdwiedzone[i]==false && kolejka.contains(i)==false)
- kolejka.add(i);
- }
- }
- }
- void odwiedz(int a){
- System.out.println("Odwiedzono :"+a+" ");
- tabOdwiedzone[a]=true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement