Homework 5 Graphs - Solutions

Homework 5 Graphs - Solutions Problem 1: Define the following terms and differentiate between the pairs. Also, draw a figure illustrating each term. a...
2 downloads 0 Views 273KB Size
Homework 5 Graphs - Solutions Problem 1: Define the following terms and differentiate between the pairs. Also, draw a figure illustrating each term. a) Directed Graph and Undirected Graph A graph is a data structure that consists of a set of nodes and a set of edges that represent connections between the nodes. In an undirected graph, the edges have no associated direction, and thus can be traversed both ways. In a directed graph, each edge has only one direction in which it can be traversed.

Undirected graph

Directed Graph

b) Path A path is a route from one node to another. If the graph has directed edges, the path can only follow the direction of the edges.

Path c) Cycle A cycle is a path where the starting node and ending node are the same.

Cycle d) Spanning Tree and Minimum Spanning Tree A spanning tree is a tree constructed from a graph that contains all of the original graphs nodes, but only a subset of its edges. Thus, the number of edges in a spanning tree is V-1, where V is the number of nodes in the original graph. A spanning tree has no cycles. A minimum spanning tree is a tree that spans the original graph along the edges with the lowest weights.

Spanning Tree

Minimum Spanning Tree

Problem 2: Let G be a graph whose vertices are the integers 1 through 8, and let the adjacency vertices of each vertex be given by the table below: Vertex 1 2 3 4 5 6 7 8

Adjacent Vertices (2,4) (1,3,4) (2,4) (1,2,3,6) (6,8) (4,5,7) (6,8) (5,7)

Assume that, in a traversal of G, the adjacent vertices of a given vertex are returned in the same order they are listed in the above table.

a) Draw G

b) Order the vertices as they are visited in a depth-first traversal starting at vertex 1 1,2,3,4,6,5,8,7 c) Order the vertices as they are visited in a breadth-first traversal starting at vertex 1 1, 2,4,3,6,5,7,8

Problem 3: Adjacency List: An adjacency list is a data structure that represents a graph by indicating which nodes the node in question is adjacent to. In a list (as opposed to a matrix) each node is stored as an object containing possibly many pointers to its neighboring nodes. Adjacency Matrix: Stores the same adjacency information as an adjacency list, but in a matrix form where the entry corresponding to the two indices is either the weight of the edge between the node (in the case of a weighted graph), or a 1 (in the case of an unweighted graph) if the nodes are adjacent. In sparse graphs, adjacency lists use less memory. It is also more efficient to read out all values in an adjacency list due to the fact that only the existing adjacent nodes must be read. In matrix form, all entries (even empty ones) must be read, taking O(n) time. If you want to determine if there is an edge between two nodes, an adjacency matrix will yield this information much faster.

Adjacency List

BOS JFK ORD MIA DFW SFO LAX BOS 0 187 867 1258 0 2704 0 JFK 187 0 740 1090 0 0 0 ORD 867 740 0 0 802 1846 0 MIA 1258 1090 0 0 1121 0 0 DFW 0 0 802 1121 0 1464 1235 SFO 2704 0 1846 0 1464 0 337 LAX 0 0 0 0 1235 337 0 Adjacency Matrix Problem 4: public class Node { String name; int neighbCnt; //This could be handled better with linked lists. //For this homework problem, arrays will be fine Node[] neighbors = new Node[4]; Node[] neighb2list = new Node[20];

public Node(String nodeName){ name = nodeName; } public void addNeighbor(Node n){ neighbors[neighbCnt]= n; neighbCnt ++; } public void distance2Neighbors(){ int neighb2cnt = 0; System.out.print("Distance-2 Neighbors: "); for(int i=0; i< neighbCnt; i++){ Node neighb = neighbors[i]; //To keep track of which airports we have already visited, //we keep a list of distance-2 neighbors. This way, we //only see each distance-2 neighbor printed out once. if(addToNeighb2List(neighb, neighb2cnt)){ neighb2cnt++;//If we just saw a new dist-2 neighbor } //Go through neighbor's neighbors for(int j= 0; j