Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Key to Homework Assignment 7: Adjacency Matrices, Lists, and Tree Traversals, Assignments of Data Structures and Algorithms

Solutions to homework assignment 7 in a data structures and algorithms course. It includes the adjacency matrix and list representations for a given graph, the depth-first and breadth-first search trees, and a proof that a free tree has n-1 edges using mathematical induction.

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-2ba
koofers-user-2ba 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
Key to Homework Assignment 7
1. (a) Draw the adjacency matrix for the graph of Figure 7.25
1 2 3 4 5 6
1 0 10 0 20 0 2
2 10 0 3 5 0 0
3 0 3 0 0 15 0
4 20 5 0 0 11 10
5 0 0 15 11 0 3
6 2 0 0 10 3 0
(b) Draw the adjacency list representation for the same graph.
1
2
3
4
5
6
24 6
10 20
2
13 4
10
3
5
23515
1
20 2 5 511
6
10
315 411 63
12410
5
3
(c) If a pointer requires 4 bytes, a vertex label requires 2 bytes, and an edge weight requires
2 bytes, which representation requires more space for this graph?
Both graphs need to store a vertex label for each of the 6 vertices:
6×2 bytes = 12 bytes.
In addition the adjacency matrix will store 36 edge labels for a total of
36 ×2 bytes + 12 bytes = 84 bytes.
1
pf3

Partial preview of the text

Download Key to Homework Assignment 7: Adjacency Matrices, Lists, and Tree Traversals and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

Key to Homework Assignment 7

  1. (a) Draw the adjacency matrix for the graph of Figure 7.

1 2 3 4 5 6 1 0 10 0 20 0 2 2 10 0 3 5 0 0 3 0 3 0 0 15 0 4 20 5 0 0 11 10 5 0 0 15 11 0 3 6 2 0 0 10 3 0 (b) Draw the adjacency list representation for the same graph.

1 2 3 4 5 6 (^2 10 4 20 )

(^1 10 3 3 )

2 3 5 15

(^1 20 2 5 5 11 6 )

(^3 15 4 11 6 )

1 2 4 10 5 3

(c) If a pointer requires 4 bytes, a vertex label requires 2 bytes, and an edge weight requires 2 bytes, which representation requires more space for this graph? Both graphs need to store a vertex label for each of the 6 vertices:

6 × 2 bytes = 12 bytes.

In addition the adjacency matrix will store 36 edge labels for a total of

36 × 2 bytes + 12 bytes = 84 bytes.

Data Structures and Algorithms, Key to Homework 7 2

There are 9 edges, and the adjacency list will store each edge weight twice. So this will increase the total for the list to

18 × 2 bytes + 12 bytes = 48 bytes.

In addition, for each of the 18 edge nodes, the list will store a pointer:

18 × 4 bytes + 48 bytes = 120 bytes.

Finally, the list will require 6 pointers for the heads of the lists, giving a grand total of

6 × 4 bytes + 120 bytes = 144 bytes.

So the adjacency list requires more space for this graph.

  1. Find the DFS tree for the graph of Figure 7.25, starting at Vertex 1. This is just a linear list. If the vertices during a call to DFS are visited in increasing order, then the list will be 1 → 2 → 3 → 5 → 4 → 6.
  2. Find the BFS tree for the graph of figure 7.25, starting at Vertex 1. If the vertices are visited in increasing order, then the tree will be

1

2 4 6

3 5

  1. Use mathematical induction on the number of vertices n to prove that a free tree has n − 1 edges. Recollect that a free tree is a connected, undirected graph with no simple cycles. Base Case. If a free tree has no vertices (n = 0), the result is false. So suppose T is a free tree with a single vertex. Since edges in undirected graphs must join distinct vertices, T can’t have any edges. So in this case we have |E| = n − 1. Induction Hypothesis. Suppose that n = n 0 ≥ 1 is a nonnegative integer such that every free tree with n nodes has n − 1 edges. Induction Step. Suppose now that T is a free tree with n = n 0 + 1 ≥ 2 vertices. Using a familiar argument, we can show that T must have at least one vertex v 0 with at most one incident edge. (v 0 is the analog of a leaf in an ordinary tree.) Furthermore, since T is connected and T contains at least 2 vertices, v 0 must have exactly one incident edge e 0. Consider the subgraph S obtained from T by removing v 0 and e 0.