Created
November 4, 2015 14:23
-
-
Save ad-m/f708b756c610f23b231d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Klasa abstrakcyjna reprezentujaca graf. | |
| */ | |
| public abstract class AGraph { | |
| protected int size; | |
| public AGraph(int vertexCount) { | |
| size = vertexCount; | |
| if (size<=0) { | |
| throw new IllegalArgumentException("Rozmiar grafu musi byc wiekszy od zera!"); | |
| } | |
| } | |
| public int getSize() { | |
| return size; | |
| } | |
| /** | |
| * Wypisuje macierz sasiedztwa grafu. | |
| */ | |
| public abstract void writeMatrix(); | |
| /** | |
| * Sprawdza, czy istnieje krawedz pomiedzy wierzcholkiem i oraz j. | |
| */ | |
| public abstract boolean check(int i, int j) throws IllegalArgumentException; | |
| /** | |
| * Tworzy krawedz pomiedzy wierzcholkiem i oraz j. | |
| */ | |
| public abstract void connect(int i, int j) throws IllegalArgumentException; | |
| /** | |
| * Wypisuje graf jako listy sasiedztwa. | |
| */ | |
| public abstract void writeList(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.LinkedList; | |
| public class ArrayListGraph extends AGraph { | |
| protected LinkedList<Integer>[] vertex; | |
| public ArrayListGraph(int vertexCount) { | |
| super(vertexCount); | |
| this.vertex = new LinkedList[vertexCount]; | |
| for (int i = 0; i < vertexCount; i++) { | |
| this.vertex[i] = new LinkedList<Integer>(); | |
| } | |
| } | |
| @Override | |
| public void writeMatrix() { | |
| for(int i=0; i<this.getSize(); i++){ | |
| System.out.print("["); | |
| for(int j=0; j<this.getSize(); j++){ | |
| System.out.print(this.check(i, j) ? "1" : "0"); | |
| } | |
| System.out.print("]\n"); | |
| } | |
| } | |
| @Override | |
| public boolean check(int i, int j) throws IllegalArgumentException { | |
| if(i > this.getSize() || j > this.getSize() || i < 0 || j < 0){ | |
| throw new IllegalArgumentException("Value out of range"); | |
| } | |
| return this.vertex[i].contains(j); | |
| } | |
| @Override | |
| public void connect(int i, int j) throws IllegalArgumentException { | |
| if(i > this.getSize() || j > this.getSize() || i < 0 || j < 0){ | |
| throw new IllegalArgumentException("Value out of range"); | |
| } | |
| this.vertex[i].add(j); | |
| } | |
| @Override | |
| public void writeList() { | |
| for(int i=0; i<this.getSize(); i++){ | |
| System.out.print(i + "->" + this.vertex[i]+ "\n"); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| ArrayListGraph graph = new ArrayListGraph(5); | |
| assert(graph.check(1, 2) == false); | |
| graph.connect(1, 2); | |
| assert(graph.check(1, 2) == true); | |
| graph.connect(1, 2); | |
| graph.connect(1, 3); | |
| graph.connect(1, 4); | |
| graph.connect(0, 3); | |
| graph.writeList(); | |
| graph.writeMatrix(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ExtraGraph extends MatrixGraph { | |
| public ExtraGraph(int vertexCount) { | |
| super(vertexCount); | |
| // TODO Auto-generated constructor stub | |
| } | |
| private boolean hasSibling(int j){ | |
| for(int i=0; i<this.getSize(); i++){ | |
| if(check(j,i) || check(i, j)){ | |
| return true; | |
| } | |
| }; | |
| return false; | |
| } | |
| public void noSibling(){ | |
| for(int i=0; i<this.getSize(); i++){ | |
| if(this.hasSibling(i)){ | |
| System.out.println(i + ","); | |
| } | |
| }; | |
| } | |
| public boolean isDirected(){ | |
| for(int i=0; i<this.getSize(); i++){ | |
| for(int j=0; j<this.getSize(); j++){ | |
| if(check(i,j) ^ check(j,i)){ | |
| return false; | |
| } | |
| } | |
| }; | |
| return true; | |
| } | |
| public boolean isClique(){ | |
| for(int i=0; i<this.vertex.length; i++){ | |
| for(int j=0; j<this.vertex.length; j++){ | |
| if(i!=j && !(check(i, j) && check(j, i))){ | |
| return false; | |
| } | |
| } | |
| } | |
| return true; | |
| } | |
| /* public ExtraGraph transposeToNew(){ | |
| ExtraGraph tmp = new ExtraGraph(this.getSize()); | |
| for(int i=0; i<this.getSize(); i++){ | |
| for(int j=0; j<this.getSize(); j++){ | |
| if(this.check(i, j)){ | |
| tmp.connect(j, i); | |
| } | |
| } | |
| } | |
| return tmp; | |
| } */ | |
| public void transpose(){ | |
| ExtraGraph tmp = new ExtraGraph(this.getSize()); | |
| for(int i=0; i<this.getSize(); i++){ | |
| for(int j=0; j<this.getSize(); j++){ | |
| if(this.check(i, j)){ | |
| tmp.connect(j, i); | |
| } | |
| } | |
| } | |
| this.vertex = tmp.vertex; | |
| } | |
| public static void main(String[] args) { | |
| ExtraGraph graph = new ExtraGraph(2); | |
| System.out.print(graph.isClique()); | |
| graph.connect(0, 1); | |
| System.out.print(graph.isClique()); | |
| graph.connect(1, 0); | |
| System.out.print(graph.isClique()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.LinkedList; | |
| public class ListListGraph extends AGraph { | |
| protected LinkedList<LinkedList<Integer>> vertex = new LinkedList<LinkedList<Integer>>(); | |
| public ListListGraph(int vertexCount) { | |
| super(vertexCount); | |
| for(int i=0; i<vertexCount; i++){ | |
| this.vertex.add(new LinkedList<Integer>()); | |
| } | |
| } | |
| @Override | |
| public void writeMatrix() { | |
| for(int i=0; i<this.getSize(); i++){ | |
| System.out.print("["); | |
| for(int j=0; j<this.getSize(); j++){ | |
| System.out.print(this.check(i, j) ? "1" : "0"); | |
| } | |
| System.out.print("]\n"); | |
| } | |
| } | |
| @Override | |
| public boolean check(int i, int j) throws IllegalArgumentException { | |
| if(i > this.getSize()|| j > this.getSize() || i < 0 || j < 0 ){ | |
| throw new IllegalArgumentException("Value out of range"); | |
| } | |
| return this.vertex.get(i).contains(j); | |
| } | |
| @Override | |
| public void connect(int i, int j) throws IllegalArgumentException { | |
| if(i > this.getSize() || j > this.getSize() || i < 0 || j < 0 ){ | |
| throw new IllegalArgumentException("Value out of range"); | |
| } | |
| this.vertex.get(i).add(j); | |
| } | |
| @Override | |
| public void writeList() { | |
| for(int i=0; i<this.getSize(); i++){ | |
| System.out.print(i + "->" + this.vertex.get(i) + "\n"); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| ListListGraph graph = new ListListGraph(5); | |
| assert(graph.check(1, 2) == false); | |
| graph.connect(1, 2); | |
| assert(graph.check(1, 2) == true); | |
| graph.connect(1, 2); | |
| graph.connect(1, 3); | |
| graph.connect(1, 4); | |
| graph.connect(0, 3); | |
| graph.writeList(); | |
| graph.writeMatrix(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MatrixGraph extends AGraph { | |
| int[][] vertex; | |
| public MatrixGraph(int vertexCount) { | |
| super(vertexCount); | |
| this.vertex = new int[vertexCount][vertexCount]; | |
| } | |
| @Override | |
| public void writeMatrix() { | |
| for(int i=0; i<this.getSize(); i++){ | |
| System.out.print("["); | |
| for(int j=0; j<this.getSize(); j++){ | |
| System.out.print(this.vertex[i][j]); | |
| } | |
| System.out.print("]\n"); | |
| } | |
| } | |
| @Override | |
| public boolean check(int i, int j) throws IllegalArgumentException { | |
| if(i > this.getSize() || j > this.getSize()){ | |
| throw new IllegalArgumentException("Value out of range"); | |
| } | |
| return (this.vertex[i][j] == 1 ? true : false); | |
| } | |
| @Override | |
| public void connect(int i, int j) throws IllegalArgumentException { | |
| if(i > this.getSize() || j > this.getSize()){ | |
| throw new IllegalArgumentException("Value out of range"); | |
| } | |
| this.vertex[i][j]=1; | |
| } | |
| @Override | |
| public void writeList() { | |
| for(int i=0; i<this.vertex.length; i++){ | |
| System.out.print(i+"=>"); | |
| for(int j=0; j<this.vertex[i].length; j++){ | |
| if(this.check(i, j)){ | |
| System.out.print(+j+","); | |
| }; | |
| } | |
| System.out.print("\n"); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| MatrixGraph graph = new MatrixGraph(5); | |
| graph.connect(1, 3); | |
| graph.connect(0, 1); | |
| graph.connect(4, 0); | |
| System.out.print("0->1 = " + graph.check(0, 1) + "\n"); | |
| graph.writeList(); | |
| graph.writeMatrix(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment