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 javaapplication10;
- import java.util.ArrayList;
- import java.util.List;
- /**
- *
- * @author Admin
- */
- public class JavaApplication10 extends LGraph{
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- JavaApplication10 t=new JavaApplication10();
- t.addVertex(); t.addVertex(); t.addVertex();t.addVertex();t.addVertex();t.addVertex();t.addVertex();
- t.addEdge(1, 2);t.addEdge(2, 4);t.addEdge(3, 2);t.addEdge(2, 5);
- t.writeList();
- //t.writeMatrix();
- //System.out.println(t.sasiedzi(2));
- System.out.println();
- LGraph w=t.transpose();
- //w.writeList();
- t.dfs();
- }
- @Override
- public void writeList() {
- for(int i=0;i<iloscWierzcholkow;i++)
- {
- System.out.println("Wierzcholek "+i+": ");
- for(int j=0;j<iloscWierzcholkow;j++)
- {
- if(check(i,j)==true)
- System.out.print(j+" ");
- }
- System.out.println();
- }
- }
- @Override
- public void writeMatrix() {
- for(int i=0;i<iloscWierzcholkow;i++)
- {
- for(int j=0;j<iloscWierzcholkow;j++)
- System.out.print(listaSasiedztwa.get(j));
- System.out.println();
- }
- }
- @Override
- public int addVertex() {
- for(int i=0;i< iloscWierzcholkow;i++)
- listaSasiedztwa.get(i).add(0);
- super.iloscWierzcholkow +=1;
- List<Integer> Lista =new ArrayList<Integer>();
- for(int i=0;i<iloscWierzcholkow;i++)
- Lista.add(0);
- listaSasiedztwa.add(Lista);
- return super.iloscWierzcholkow;
- }
- @Override
- public void addEdge(int source, int target) throws IllegalArgumentException {
- listaSasiedztwa.get(source).set(target,1);
- }
- @Override
- public List<Integer> sasiedzi(int v) throws IllegalArgumentException {
- return listaSasiedztwa.get(v);
- }
- @Override
- public boolean check(int i, int j) throws IllegalArgumentException {
- if(listaSasiedztwa.get(i).get(j)==1)
- return true;
- else
- return false;
- }
- List<Boolean> visited;
- @Override
- public void dfs() {
- visited= new ArrayList<Boolean>();
- for(int i=0;i<iloscWierzcholkow;i++)
- visited.add(Boolean.FALSE);
- for(int i=1;i<iloscWierzcholkow;i++)
- {
- if(visited.get(i)==false)
- odwiedzaj(i);
- }
- }
- @Override
- protected void odwiedzaj(int v) {
- visited.set(v,true);
- System.out.println("Odwiedzam :"+v);
- for(int i=0;i<iloscWierzcholkow;i++)
- if(!visited.get(i) && check(v,i))
- odwiedzaj(i);
- }
- @Override
- public LGraph transpose() {
- LGraph l=new JavaApplication10();
- //l.iloscWierzcholkow=super.iloscWierzcholkow;
- for(int i=0;i<iloscWierzcholkow;i++)
- l.addVertex();
- for(int i=0;i<iloscWierzcholkow;i++)
- for(int j=0;j<iloscWierzcholkow;j++)
- if(check(i,j)==true)
- {
- l.addEdge(j,i);
- }
- return l;
- }
- }
Add Comment
Please, Sign In to add comment