Presentation is loading. Please wait.

Presentation is loading. Please wait.

22C:21 Problem 2.3 Solution Outline.

Similar presentations


Presentation on theme: "22C:21 Problem 2.3 Solution Outline."— Presentation transcript:

1 22C:21 Problem 2.3 Solution Outline

2 Data Structure to store
public class sparseMatrix { int row; int column; sparseMatrix (int r, int c) { this.row = r; this.column = c; }

3 The compress( ) Method public void compress() {
// sparseMatrix[] compressed = new sparseMatrix[numEdges]; 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. }

4 Main( ) in sparseGraph class
public static void main(String[] args) { sparseGraph G = new sparseGraph(100); // Adding 100 vertices for(int i=0;i<100;i++) G.addVertex(Integer.toString(i)); // Adding 4 edges G.addEdge("12", "25"); G.addEdge("12", "19"); G.addEdge("29", "45"); G.addEdge("99", "56"); G.compress(); }

5 Symmetric search area Ignore No self-loop Search

6 Output The compressed form: (19,12) (25,12) (45,29) (99,56)

7 Quiz 2: Problem 2.2 (modified)
Suppose that we use the myGraph 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 myGraph class: names, Edges, numVertices, and numEdges. Make sure you follow the code in the myGraph 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.


Download ppt "22C:21 Problem 2.3 Solution Outline."

Similar presentations


Ads by Google