DATA STURCTURES - Post Midterm 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/39

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:53 PM on 5/9/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

40 Terms

1
New cards

Priority queues are implemented with a…

binary heap

2
New cards

Properties of a binary heap

A binary heap is always a complete binary tree:

1) Every level of the tree is completely filled, except the last which should be filled from left to right

2) Largest key is a[1]

3) Can use array indices to move through tree —> Parent of node at k is k/2 and children of node at k are at 2k and 2k+1

3
New cards

In a binary heap, the parent’s key must be ________________ than children’s keys

no smaller

4
New cards

How do you insert into a binary heap?

Add node at the end, then swim it up

Note: May need to swim because the new element can have a larger key than its parent

<p>Add node at the end, then <strong>swim</strong> it up</p><p>Note: May need to swim because the new element can have a larger key than its parent</p>
5
New cards

How does swim work in binary heap?

For example, for a MAX heap:

1) Identify the parent

2) If the value of the child is greater than the parent, swap

3) Once parent is greater than child, stop

6
New cards

How does sink work in binary heap?

1) Identify the largest child

2) If the parent is smaller than the largest child, swap

3) Repeats until parent is larger than children

7
New cards

How do you demote a key in a binary heap?

Exchange key in parent with key in larger child until heap order is restored (sink)

Note: May have to sink when a key becomes smaller than one or both of its children

<p>Exchange key in parent with key in larger child until heap order is restored (<strong>sink</strong>)</p><p>Note: May have to sink when a key becomes smaller than one or both of its children</p>
8
New cards

How to delete the maximum in a binary heap?

Exchange root with node at end, then sink it down

<p>Exchange root with node at end, then sink it down</p>
9
New cards

What is the max-heap order invariant?

The key in each node is greater than or equal to all its children keys

10
New cards

What is the running time of insert and delete-max in a binary heap?

Both are log(n)

11
New cards

Heapsort

1) View input array as a complete binary tree

2) Heap construction: Build a max-heap with all n keys

3) Sortdown: Repeatedly remove the maximum key

<p>1) View input array as a complete binary tree</p><p>2) Heap construction: Build a max-heap with all <em>n</em> keys</p><p>3) Sortdown: Repeatedly remove the maximum key</p>
12
New cards

What is the best, average, and worst case for heapsort?

O(n log n) for all three cases.

Note: n log n guarantee and in-place sorting algorithm

13
New cards

Heapsort is optimal for both time and space, but…

1) Inner loop longer than quicksort’s

2) Makes poor use of cache

3) Not stable

14
New cards

Separate-chaining symbol table

Uses an array of m linked lists:

1) Hash: Map key to integer i between 0 and m - 1

2) Insert: Put at front of ith chain (if not already in chain)

3) Search: Sequential search in ith chain

<p>Uses an array of <em>m</em> linked lists:</p><p>1) Hash: Map key to integer <em>i</em> between 0 and <em>m</em> - 1</p><p>2) Insert: Put at front of <em>i</em>th chain (if not already in chain)</p><p>3) Search: Sequential search in <em>i</em>th chain</p>
15
New cards

When resizing a hash table, what happens to all keys?

All keys must be rehashed

16
New cards

Linear-probing hash table

1) Hash: Map key to integer i between 0 and m - 1

2) Insert: Put at table index i if free; if not try i + 1, i + 2, etc.

3) Search table index i; if occupied but no match, try i + 1, i + 2, etc.

Note: Array length m must be greater than number of key-value pairs n

17
New cards

What is the running time of separate chaining and linear probing?

18
New cards

A graph is a set of vertices connected pairwise by _______.

edges

19
New cards

Two vertices are connected if…

there is a path between them

20
New cards

In an undirected graph, what is the maximum number of edges on a Graph of V vertices?

V * (V-1)/2

21
New cards

In a directed graph, what is the maximum number of edges on a Graph of V vertices?

V * (V-1)

22
New cards

Depth-first search

To visit a vertex v:

1) Mark vertex v

2) Recursively visit all unmarked vertices adjacent to v

Note: Uses a Stack and goes as deep as possible down one path before backtracking

23
New cards

What data structures are used in depth-first search?

1) Boolean array marked[] to mark vertices

2) Integer array edgeTo[] to keep track of paths (edgeTo[w] == v) means that edge v-w taken to discover vertex w

3) Function-call stack for recursion

24
New cards

Breadth-first search

Repeat until queue is empty:
1) Remove vertex v from queue

2) Add to queue all unmarked vertices adjacent to v and mark them

Note: Explores all neighbors at the current depth before moving to the next level

<p>Repeat until queue is empty:<br>1) Remove vertex <em>v</em> from queue</p><p>2) Add to queue all unmarked vertices adjacent to <em>v</em> and mark them</p><p></p><p>Note: Explores all neighbors at the current depth before moving to the next level</p>
25
New cards

In any connected graph G, BFS computes __________ paths from s to all other vertices in time proportional to E + V

shortest

26
New cards

Which is order of growth of running time of removing an edge v→w from a digraph uses the adjacency-lists representation, where V is the number of vertices and E is the number of edges?

outdegree(v)

27
New cards

Which is order of growth of running time of the following code fragment ifthe digraph uses the adjacency-lists representation, where V is the number of vertices and E is the number of edges?

E + V

28
New cards

What is the adjacency list representation of a directed graph?

A vertex-indexed array of lists

<p>A vertex-indexed array of lists</p>
29
New cards

What is an in-degree in directed graphs?

The number of edges pointing into a vertex

30
New cards

What is an out-degree in directed graphs?

The number of edges pointing out of a vertex

31
New cards

Directed acyclic graph (DAG)

Directed graph with no cycles

32
New cards

Topological sort

1) Start with DFS

2) Move to an unvisited neighbor until there is a node that has no unvisited neighbors left

3) Push that node onto the stack

4) Go back to previous node and se eif it has other unvisited neighbors. If not, push it onto the stack

Once every node is visited, reverse postorder is simply the elements as you pop them off the Stack

<p>1) Start with DFS</p><p>2) Move to an unvisited neighbor until there is a node that has no unvisited neighbors left</p><p>3) Push that node onto the stack</p><p>4) Go back to previous node and se eif it has other unvisited neighbors. If not, push it onto the stack</p><p></p><p>Once every node is visited, reverse postorder is simply the elements as you pop them off the Stack</p>
33
New cards

In topological sort, how do you find the stopping and starting points?

Vertices with outdegree = 0 will always be the stopping points

Vertices with indegree = 0 should be at the front of the order

34
New cards

What are the steps to heapify an unsorted array?

1) Find the last parent at index i = n/2

2) Compare the parent to its left child (= 2i) and right child (= 2i + 1). Then sink or swim as needed

3) If a sink or swim was performed, check that max-heap or min-heap properties are still valid and continue to sink or swim if needed

4) Once that node is settled, move to the previous index (= i - 2) and repeat

35
New cards

How does the sortdown algorithm work?

1) Swap the root (at index 1) with the last element in the active heap —> This puts the largest number in its final sorted position

2) The last element is no longer in the “active” heap, it is no longer touched again for the rest of the sort

3) Sink the new root/first element to its proper level —> Compare the root with its children and swap with the larger child

Recall that left child = 2i and right child = 2i + 1

36
New cards
<p>On the undirected <strong>and</strong> directed graph implementation, what is the<strong> worst-case running time for the space needed in</strong>: list of edges, adjacency matrix, adjacency lists?</p>

On the undirected and directed graph implementation, what is the worst-case running time for the space needed in: list of edges, adjacency matrix, adjacency lists?

List of edges: E

Adjacency matrix: V2

Adjacency lists: E + V

37
New cards
<p>On the undirected <strong>and</strong> directed graph implementation, <strong>what is the worst-case running time for checking if w is adjacent to v in</strong>: list of edges, adjacency matrix, adjacency lists?</p>

On the undirected and directed graph implementation, what is the worst-case running time for checking if w is adjacent to v in: list of edges, adjacency matrix, adjacency lists?

List of edges: E

Adjacency matrix: 1

Adjacency list: degree(v)

38
New cards
<p>On the <strong>undirected</strong> graph implementation, <strong>what is the worst-case running time for determining the degree of v in</strong>: list of edges, adjacency matrix, adjacency lists?</p>

On the undirected graph implementation, what is the worst-case running time for determining the degree of v in: list of edges, adjacency matrix, adjacency lists?

List of edges: E

Adjacency matrix: V

Adjacency lists: degree(V)

39
New cards
<p>On the <strong>directed</strong> graph implementation, <strong>what is the worst-case running time for determining the degree of v in</strong>: list of edges, adjacency matrix, adjacency lists?</p>

On the directed graph implementation, what is the worst-case running time for determining the degree of v in: list of edges, adjacency matrix, adjacency lists?

List of edges: Out-degree and in-degree is both E

Adjacency matrix: Out-degree and in-degree is both V

Adjacency list: Out-degree = degree(v) and in-degree = E + V

40
New cards

Depth-First Search (DFS) populates two data structures marked[] and edgeTo[]. List three distinct characteristics of an undirected graph that you could determine based on the results of examining those data structures after DFS runs once from a vertex s.

1) Is the graph connected

2) Is there a path from the starting vertex to a given vertex

3) What is a path from the starting vertex to a given vertex (if one exists)