Question: 1- Print all the vertices connected to Node 1 in the graph //write the code that Print all the vertices connected to Node 1 in

1- Print all the vertices connected to Node 1 in the graph

//write the code that Print all the vertices connected to Node 1 in the graph

public static void main(String[] args) { // TODO code application logic here WeightedGraph G = new WeightedGraph(10); G.addVertex(1); G.addVertex(2); G.addVertex(4); G.addVertex(5); G.addVertex(6); G.addVertex(7); G.addVertex(9);

G.addEdge(1, 5, 1); G.addEdge(1, 6, 1); G.addEdge(5, 7, 1); G.addEdge(6, 7, 1); G.addEdge(2, 6, 1); G.addEdge(2, 7, 1); G.addEdge(2, 9, 1); G.addEdge(9, 4, 1);

// Q1

// Q2

// Q3

}

public class WeightedGraph> implements WeightedGraphInterface {

private int numVertices; private int maxVertices; private T[] vertices; private int[][] edges; private boolean[] marks; // marks[i] is mark for vertices[i] private int maxSize;

public WeightedGraph(int maxV) { maxSize = maxV; numVertices = 0; maxVertices = maxV; vertices = (T[]) new Comparable[maxV]; marks = new boolean[maxV]; edges = new int[maxV][maxV]; }

//WeightedGraph(int i) { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. //}

public boolean isEmpty() { return numVertices == 0; }

public boolean isFull() { return numVertices == maxSize; }

public void addVertex(T vertex) { vertices[numVertices] = vertex; for (int index = 0; index < numVertices; index++) { edges[numVertices][index] = 0; edges[index][numVertices] = 0; } numVertices++; }

public boolean hasVertex(T vertex) { for (int i = 0; i < numVertices; i++) { if (vertex.compareTo(vertices[i]) == 0) { return true; } } return false; }

private int indexIs(T vertex) { int index = 0; while (vertex.compareTo(vertices[index]) != 0) { index++; } return index; }

public void addEdge(T fromVertex, T toVertex, int weight) // Adds an edge with the specified weight from fromVertex to toVertex. { int row; int column; row = indexIs(fromVertex); column = indexIs(toVertex); edges[row][column] = weight; edges[column][row] = weight; }

public int weightIs(T fromVertex, T toVertex) { int row; int column; row = indexIs(fromVertex); column = indexIs(toVertex); return edges[row][column]; }

public QueueList getToVertices(T vertex) { QueueList adjVertices = new QueueList(); int fromIndex; int toIndex; fromIndex = indexIs(vertex); for (toIndex = 0; toIndex < numVertices; toIndex++) { if (edges[fromIndex][toIndex] != 0) { adjVertices.enqueue(vertices[toIndex]); } } return adjVertices; }

public void clearMarks() { for (int i = 0; i < numVertices; i++) { marks[i] = false; } }

public void markVertex(T vertex) { for (int i = 0; i < numVertices; i++) { if (vertex.compareTo(vertices[i]) == 0) { marks[i] = true; return; } } }

public boolean isMarked(T vertex) { for (int i = 0; i < numVertices; i++) { if (vertex.compareTo(vertices[i]) == 0) { return marks[i]; } } return false; }

public T getUnmarked() { for (int i = 0; i < numVertices; i++) { if (marks[i] == false) { return vertices[i]; } } return null; }

public boolean edgeExists(T vertex1, T vertex2) { return (edges[indexIs(vertex1)][indexIs(vertex2)] != 0); }

public boolean removeEdge(T vertex1, T vertex2) { boolean existed = edgeExists(vertex1, vertex2); edges[indexIs(vertex1)][indexIs(vertex2)] = 0; return existed; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!