Вы находитесь на странице: 1из 2

Question 1: Given the following directed graph, how would you represent it with an edge list?

List the edges in the space below. The order of the edges doesn't matter, but the order of the vertices in each edge
does matter, since this graph is directed.

Question 2: Given the following directed graph, how would you represent it with an adjacency matrix?

Fill out this matrix of 0s and 1s, where the entry in row i and column j is 1 if and only if the edge (i,j) is in the
graph. Since this is a directed graph, the matrix will not necessarily be symmetric.

Question 3: Given the following directed graph, how would you represent it with an adjacency list?
Question 4: Consider the following graph,

Part a: Store an adjacency matrix


We've stored a graph, with 6 vertices indexed 0-5, as an edge list in the variable
edgeList. Store the same graph, as an adjacency matrix, in the variable adjMatrix.

import java.util.Arrays;

class Solution {
public static int[][] edgeList = new int[][] {
new int[] {0, 2},
new int[] {1, 3},
new int[] {2, 3},
new int[] {2, 4},
new int[] {3, 5},
new int[] {4, 5}
};

// Fill in this adjMatrix to represent the graph


public static int[][] adjMatrix = null;

}
Part 2: Store an adjacency list

Store the same graph, as an adjacency list, in the variable adjList.

import java.util.Arrays;

class Solution {
public static int[][] edgeList = new int[][] {
new int[] {0, 2},
new int[] {1, 3},
new int[] {2, 3},
new int[] {2, 4},
new int[] {3, 5},
new int[] {4, 5}
};

// Fill in this adjList to represent the graph


public static int[][] adjList = null;

Вам также может понравиться