Advertisement
Guest User

Lab 1 objektorienterad modellering och design

a guest
Jan 20th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. package lab1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6.  
  7. import graph.Graph;
  8. import graph.SimpleGraph;
  9.  
  10. public class Lab1 {
  11.     /**
  12.      * Returns the number of vertices in the graph g.
  13.      */
  14.     public static int vertexCount(Graph<Integer> g) {
  15.         // TODO(D1): implement this!
  16.         return g.vertexCount();
  17.     }
  18.  
  19.     /**
  20.      * Returns the number of edges in the graph g.
  21.      */
  22.     public static int edgeCount(Graph<Integer> g) {
  23.         // TODO(D2): implement this!
  24.         int edges = 0;
  25.         Collection<Integer> list = g.vertexSet();
  26.  
  27.         for (Integer i : list) {
  28.  
  29.             edges += g.neighbours(i).size();
  30.         }
  31.         return edges;
  32.     }
  33.  
  34.     /**
  35.      * Returns true if there is an edge from vertex u to vertex v. Returns false if
  36.      * u and v are not connected or if there is only an edge from v to u.
  37.      *
  38.      * @param g a graph containing u and v
  39.      * @param u index of the first vertex in g
  40.      * @param v index of the second vertex in g
  41.      */
  42.     public static boolean edgeBetween(Graph<Integer> g, int u, int v) {
  43.         // TODO(D3): implement this!
  44.  
  45.         Collection<Integer> list = g.neighbours(u);
  46.  
  47.         for (Integer i : list) {
  48.  
  49.             if (i.equals(v)) {
  50.                 return true;
  51.             }
  52.         }
  53.         return false;
  54.     }
  55.  
  56.     /**
  57.      * Returns a simple graph with at least 6 vertices and at least 10 edges.
  58.      */
  59.     public static Graph<Integer> buildGraph() {
  60.         // TODO(D5): implement this!
  61.        
  62.         Graph<Integer> graph = new SimpleGraph(7, new int[][] {
  63.             {0, 1},
  64.             {0, 2},
  65.             {1, 2},
  66.             {2, 3},
  67.             {3, 4},
  68.             {4, 5},
  69.             {4, 2},
  70.             {5, 6},
  71.             {6, 0},
  72.             {6, 1},
  73.         });
  74.        
  75.         return graph;
  76.     }
  77. }
  78.  
  79. ///////////////////////////NEW CLASS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  80.  
  81. package lab1;
  82.  
  83. import graph.Graph;
  84. import graph.SimpleGraph;
  85. import org.junit.Test;
  86.  
  87. import static com.google.common.truth.Truth.assertThat;
  88.  
  89. public class TestEdgeBetween {
  90.  
  91.   @Test public void singleEdge() {
  92.     Graph<Integer> g = new SimpleGraph(10, new int[][] {{4, 7}});
  93.     assertThat(Lab1.edgeBetween(g, 4, 7)).isTrue();
  94.     assertThat(Lab1.edgeBetween(g, 7, 4)).isFalse();
  95.   }
  96.  
  97.   @Test public void simplePath() {
  98.     Graph<Integer> g = new SimpleGraph(5, new int[][] {
  99.         {0, 1},
  100.         {1, 2},
  101.         {2, 3},
  102.         {3, 4},
  103.     });
  104.     assertThat(Lab1.edgeBetween(g, 0, 1)).isTrue();
  105.     assertThat(Lab1.edgeBetween(g, 1, 2)).isTrue();
  106.     assertThat(Lab1.edgeBetween(g, 2, 3)).isTrue();
  107.     assertThat(Lab1.edgeBetween(g, 3, 4)).isTrue();
  108.     assertThat(Lab1.edgeBetween(g, 4, 0)).isFalse();
  109.     assertThat(Lab1.edgeBetween(g, 0, 2)).isFalse();
  110.     assertThat(Lab1.edgeBetween(g, 0, 3)).isFalse();
  111.     assertThat(Lab1.edgeBetween(g, 0, 4)).isFalse();
  112.     assertThat(Lab1.edgeBetween(g, 1, 0)).isFalse();
  113.     assertThat(Lab1.edgeBetween(g, 2, 0)).isFalse();
  114.     assertThat(Lab1.edgeBetween(g, 3, 2)).isFalse();
  115.     assertThat(Lab1.edgeBetween(g, 4, 1)).isFalse();
  116.   }
  117.  
  118.   @Test public void loop() {
  119.     Graph<Integer> g = new SimpleGraph(5, new int[][] {
  120.         {0, 1},
  121.         {1, 2},
  122.         {2, 3},
  123.         {3, 4},
  124.         {4, 0}
  125.     });
  126.     assertThat(Lab1.edgeBetween(g, 0, 1)).isTrue();
  127.     assertThat(Lab1.edgeBetween(g, 1, 2)).isTrue();
  128.     assertThat(Lab1.edgeBetween(g, 2, 3)).isTrue();
  129.     assertThat(Lab1.edgeBetween(g, 3, 4)).isTrue();
  130.     assertThat(Lab1.edgeBetween(g, 4, 0)).isTrue();
  131.     assertThat(Lab1.edgeBetween(g, 0, 2)).isFalse();
  132.     assertThat(Lab1.edgeBetween(g, 0, 3)).isFalse();
  133.     assertThat(Lab1.edgeBetween(g, 0, 4)).isFalse();
  134.     assertThat(Lab1.edgeBetween(g, 1, 0)).isFalse();
  135.     assertThat(Lab1.edgeBetween(g, 2, 0)).isFalse();
  136.     assertThat(Lab1.edgeBetween(g, 3, 2)).isFalse();
  137.     assertThat(Lab1.edgeBetween(g, 4, 1)).isFalse();
  138.   }
  139.  
  140.   @Test public void empty() {
  141.         Graph<Integer> g = new SimpleGraph(0, new int[][] {
  142.  
  143.            
  144.         });
  145.        
  146.         assertThat(g.vertexCount() == 0);
  147.         assertThat(Lab1.edgeBetween(g, 0, 1)).isFalse();
  148.         assertThat(Lab1.edgeBetween(g, 1, 2)).isFalse();
  149.         assertThat(Lab1.edgeBetween(g, 2, 1)).isFalse();
  150.        
  151.       }
  152.  
  153.   @Test public void edgeBetweenSelf() {
  154.         Graph<Integer> g = new SimpleGraph(6, new int[][] {
  155.  
  156.             {0, 1},
  157.             {1, 2},
  158.             {2, 3},
  159.             {3, 4},
  160.             {4, 0}
  161.            
  162.         });
  163.        
  164.         assertThat(Lab1.edgeBetween(g, 0, 1)).isTrue();
  165.         assertThat(Lab1.edgeBetween(g, 3, 4)).isTrue();
  166.         assertThat(Lab1.edgeBetween(g, 0, 0)).isFalse();
  167.         assertThat(Lab1.edgeBetween(g, 1, 1)).isFalse();
  168.         assertThat(Lab1.edgeBetween(g, 2, 2)).isFalse();
  169.        
  170.       }
  171.  
  172.   @Test public void manyEdges() {
  173.         Graph<Integer> g = new SimpleGraph(6, new int[][] {
  174.  
  175.             {0, 1},
  176.             {0, 2},
  177.             {0, 3},
  178.             {0, 4},
  179.             {0, 5}
  180.            
  181.         });
  182.        
  183.         assertThat(Lab1.edgeBetween(g, 0, 1)).isTrue();
  184.         assertThat(Lab1.edgeBetween(g, 0, 2)).isTrue();
  185.         assertThat(Lab1.edgeBetween(g, 0, 3)).isTrue();
  186.         assertThat(Lab1.edgeBetween(g, 0, 4)).isTrue();
  187.         assertThat(Lab1.edgeBetween(g, 0, 5)).isTrue();
  188.         assertThat(Lab1.edgeBetween(g, 0, 6)).isFalse();
  189.        
  190.       }
  191.  
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement