22 C 21 Problem 2 3 Solution Outline
22 C: 21 Problem 2. 3 Solution Outline
Data Structure to store public class sparse. Matrix { int row; int column; sparse. Matrix (int r, int c) { this. row = r; this. column = c; } }
The compress( ) Method public void compress() { // sparse. Matrix[] compressed = new sparse. Matrix[num. Edges]; System. out. println("The compressed form: "); // Search and print for(int i = 0; i < Edges. length; i++) for(int j = 0; j < i; j++) if(Edges[i][j] == true) System. out. println("(“ + i + ", “ + j + ")"); // It makes sense to store and return the array, // But for testing purposes we will just print here. }
Main( ) in sparse. Graph class public static void main(String[] args) { sparse. Graph G = new sparse. Graph(100); // Adding 100 vertices for(int i=0; i<100; i++) G. add. Vertex(Integer. to. String(i)); // Adding 4 edges G. add. Edge("12", "25"); G. add. Edge("12", "19"); G. add. Edge("29", "45"); G. add. Edge("99", "56"); G. compress(); }
Symmetric search area Ignore No self-loop Search
Output The compressed form: (19, 12) (25, 12) (45, 29) (99, 56)
Quiz 2: Problem 2. 2 (modified) Suppose that we use the my. Graph class to build a graph. We start with an empty graph and first add vertices in the order: a, b, c, d, e, f, g. Then we add edges in the order {a, b}, {a, c}, {a, d}, {b, f}, {a, f}, {d, e}, {g, f}. After all of these additions show the contents of the four data members of the my. Graph class: names, Edges, num. Vertices, and num. Edges. Make sure you follow the code in the my. Graph class carefully and show the contents of the variables exactly. Make sure you pay attention to how the arrays are resized. Draw the final graph.
- Slides: 7