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 grafy;
- /**
- *
- * @author Stud
- */
- public class TGraph extends AGraph {
- int[][] matrix;
- public TGraph(int vertexCount) {
- super(vertexCount);
- matrix = new int [100][100];
- }
- @Override
- public void writeMatrix() {
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- System.out.printf(matrix[i][j] + " ");
- }
- System.out.println("");
- }
- }
- @Override
- public boolean check(int i, int j) throws IllegalArgumentException {
- if (i < 0 || i >= size) {
- throw new IllegalArgumentException("złe dane");
- }
- if (j < 0 || j >= size) {
- throw new IllegalArgumentException("złe dane");
- } else {
- if (matrix[i][j] == 1) {
- return true;
- } else {
- return false;
- }
- }
- }
- @Override
- public void connect(int i, int j) throws IllegalArgumentException {
- if (i < 0 || i >= size) {
- throw new IllegalArgumentException("złe dane");
- }
- if (j < 0 || j >= size) {
- throw new IllegalArgumentException("złe dane");
- } else {
- matrix[i][j] = 1;
- }
- }
- @Override
- public void writeList() {
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- if (check(i, j)) {
- System.out.println("(" + i + "," + j + ")");
- }
- }
- }
- }
- public void czyMaSasiada() {
- int s = 0;
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- if (check(i, j)) {
- break;
- } else {
- s += 1;
- }
- }
- if (s == size) {
- System.out.println(i + " Nie mam sasiada");
- }
- s=0;
- }
- }
- public void klika()
- {
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- if (matrix[i][j] == 0 && i!=j){
- System.out.println("Graf nie jest klika");
- return;
- } else { System.out.println("Graf jest kliką");
- return; }
- }
- }
- }
- public void transpozycja() {
- System.out.println("Macierz po transpozycji: ");
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- System.out.printf(matrix[j][i] + " ");
- }
- System.out.println("");
- }
- }
- public static void main(String[] args) {
- TGraph graf = new TGraph(5);
- graf.connect(1,4);
- graf.connect(1,2);
- graf.connect(2,3);
- graf.connect(1,1);
- graf.connect(3,4);
- graf.connect(2,1);
- graf.connect(4,2);
- graf.writeMatrix();
- System.out.println("Czy krawędź "+graf.check(1,4));
- System.out.println(" ");
- graf.writeList();
- graf.klika();
- graf.transpozycja();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement