Advertisement
Guest User

Grafo - Planaridade

a guest
Oct 1st, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 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 péquége;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. /**
  13.  *
  14.  * @author mathias
  15.  */
  16. public class Planar {
  17.     Grafo grafo;
  18.    
  19.     public Planar(Grafo grafo){
  20.         this.grafo = grafo;
  21.     }
  22.    
  23.     public boolean isPlanar(){
  24.         if(grafo.getNumVertices() > 2 && isCircular(grafo.getMatrizAdjacencia())){
  25.             if (grafo.getNumArestas() <= 3 * grafo.getNumVertices() - 6) {
  26.                 return true;
  27.             }
  28.         }else if (grafo.getNumVertices() < 3){
  29.             return true;
  30.         }else if(grafo.getNumArestas() <= 2 * grafo.getNumVertices() - 4){
  31.             return true;
  32.         }
  33.         return false;
  34.     }
  35.    
  36.     public boolean isCircular(int[][] matriz){
  37.         for (int i = 0; i < matriz.length; i++) {
  38.             List<Integer> adjacencias = new ArrayList<>();
  39.             for (int j = 0; j < matriz.length; j++) {
  40.                 if (matriz[i][j] > 0) {
  41.                     adjacencias.add(j);
  42.                 }
  43.             }
  44.             if (adjacencias.size() > 1) {
  45.                 while(!adjacencias.isEmpty()){
  46.                     for (int k = 1; k < adjacencias.size(); k++) {
  47.                         if (matriz[adjacencias.get(0)][adjacencias.get(k)] > 0) {
  48.                             return true;
  49.                         }
  50.                     }
  51.                     adjacencias.remove(0);
  52.                 }  
  53.             }
  54.         }
  55.         return false;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement