0% found this document useful (0 votes)
153 views2 pages

Worksheet 7 Graphs

The document discusses representing a directed graph using different data structures: an edge list, adjacency matrix, and adjacency list. It provides examples of directed graphs and asks the reader to represent each graph using these different structures. It also includes code snippets where the reader is asked to fill in the appropriate data structure to represent a given graph stored in an edge list.

Uploaded by

Kashif Amjad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views2 pages

Worksheet 7 Graphs

The document discusses representing a directed graph using different data structures: an edge list, adjacency matrix, and adjacency list. It provides examples of directed graphs and asks the reader to represent each graph using these different structures. It also includes code snippets where the reader is asked to fill in the appropriate data structure to represent a given graph stored in an edge list.

Uploaded by

Kashif Amjad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy