COM1009 Algorithms and Data Structures

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

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 11:19 AM on 6/11/25
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

30 Terms

1
New cards

Insertion Sort Algorithm

Runtime:

  • Best case: Sorted ascending => n-1

  • Worst case: Sorted descending =>

<p>Runtime:</p><ul><li><p>Best case: Sorted ascending =&gt; n-1</p></li><li><p>Worst case: Sorted descending =&gt;</p></li></ul><p></p>
2
New cards

Merge Sort

A divide and conquer algorithm that sorts an array by recursively dividing it into halves, sorting each half, and then merging the sorted halves back together.

<p>A divide and conquer algorithm that sorts an array by recursively dividing it into halves, sorting each half, and then merging the sorted halves back together. </p>
3
New cards

Priority Queue Naïve Implementation

Each element x of a set M, has priority x.key. Array position containing the highest priority is at A[iM] which is checked each time an element is inserted into an array with a priority (Insert(priority p)), FindMax() returns A[iM]. ExtractMax() removes the element with highest priority and sets the new maximum as needed.

<p>Each element x of a set M, has priority x.key.  Array position containing the highest priority is at A[iM] which is checked each time an element is inserted into an array with a priority (Insert(priority p)), FindMax() returns A[iM]. ExtractMax() removes the element with highest priority and sets the new maximum as needed.</p>
4
New cards

Heap

A heap is an array corresponding to a binary tree, for which all levels are full except the last and is filled left to right.

A heap ‘A’

  • Has the max-heap property,, if…

  • For every node i > 1

  • A[parent(i)] >= A[i]

  • Called a max-heap

  • Has the min-heap property if…

  • For every node i > 1

  • A[parent(i)] <= A[i]

  • Called a min-heap

5
New cards

MaxHeapify

Is a procedure to maintain the max-heap property in a binary heap. It ensures that the subtree rooted at a given node follows the property by comparing the node with its children and swapping if necessary.

Trees are stored in arrays where the child nodes of an element are at index x2 and index x2 +1

<p>Is a procedure to maintain the max-heap property in a binary heap. It ensures that the subtree rooted at a given node follows the property by comparing the node with its children and swapping if necessary. </p><p>Trees are stored in arrays where the child nodes of an element are at index x2 and index x2 +1</p>
6
New cards

Heap Sort Algorithm

A comparison-based sorting algorithm that utilizes a binary heap data structure to sort elements. It first builds a max-heap from the input data and then repeatedly extracts the maximum element from the heap, placing it at the end of the sorted array.

<p>A comparison-based sorting algorithm that utilizes a binary heap data structure to sort elements. It first builds a max-heap from the input data and then repeatedly extracts the maximum element from the heap, placing it at the end of the sorted array. </p>
7
New cards

Probability Modelling Trick

knowt flashcard image
8
New cards

Quicksort

knowt flashcard image
9
New cards

Growth Orders

knowt flashcard image
10
New cards

CountingSort

A linear time algorithm

  • For every x of the input, count numbers <= x

  • Use this to write x into correct position in output

  • A - input array

  • Z - output arrat

  • C - work array (where we count)

  • K - limit of the universe: {0,..,K}

Runtime for n numbers: O(n + k)

<p>A linear time algorithm</p><ul><li><p>For every x of the input, count numbers &lt;= x</p></li><li><p>Use this to write x into correct position in output</p></li><li><p>A - input array</p></li><li><p>Z - output arrat</p></li><li><p>C - work array (where we count)</p></li><li><p>K - limit of the universe: {0,..,K}</p></li></ul><p></p><p>Runtime for n numbers: O(n + k)</p><p></p>
11
New cards

In-Order Traversal

  • Recursively travel left subtree of the root

  • Output value of the root

  • Recursively traverse the right subtree of the root

<ul><li><p>Recursively travel left subtree of the root</p></li><li><p>Output value of the root</p></li><li><p>Recursively traverse the right subtree of the root</p></li></ul><p></p>
12
New cards

Red-Black trees

BST with the following properties:

  • Each node either red (bad) or black (good)

  • Root is black

  • All leaves are black and are nil

  • If a rode is red - its children are black

  • For each subtree - all root-to-leaf paths have the same number of black nodes

  • No red to leaf path has consecutive red nodes

  • The height h(v) of a node v is the number of nodes on the longest path from v to a leaf below v (excluding v)

  • The black-height bh(v) of node v is the number of black nodes on each path from v to a leaf below v (excluding v)

<p>BST with the following properties:</p><ul><li><p>Each node either red (bad) or black (good)</p></li><li><p>Root is black</p></li><li><p>All leaves are black and are nil</p></li><li><p>If a rode is red - its children are black</p></li><li><p>For each subtree - all root-to-leaf paths have the same number of black nodes</p></li><li><p>No red to leaf path has consecutive red nodes</p></li><li><p>The height h(v) of a node v is the number of nodes on the longest path from v to a leaf below v (excluding v)</p></li><li><p>The black-height bh(v) of node v is the number of black nodes on each path from v to a leaf below v (excluding v)</p></li></ul><p></p>
13
New cards

Inserting Nodes in BSTs

knowt flashcard image
14
New cards

RBInsertFixup(Node z)

knowt flashcard image
15
New cards

BSTs - Node Successor

knowt flashcard image
16
New cards

BSTs - Deleting nodes

knowt flashcard image
17
New cards

RBTs - Deleting nodes

  • Y - points to node that is deleted/removed

  • X - points to node that replaces y. Which is either the only child of y or nil

<ul><li><p>Y - points to node that is deleted/removed</p></li><li><p>X - points to node that replaces y. Which is either the only child of y or nil</p></li></ul><p></p>
18
New cards

RBDeleteFixup

Improvement of delete method to avoid violations of colour rule

<p>Improvement of delete method to avoid violations of colour rule</p>
19
New cards

Greedy Algorithm - Iterative

General Greedy Strategy

  1. Check if problem exhibits the optimal substructure property

  2. Develop a recursive solution

  3. Show that with a greedy choice, only one sub-instance remains

  4. Show that the greedy choice is ‘safe’ (exchange argument)

  5. Design a recursive greedy algorithm

  6. Convert to an iterative algorithm

<p>General Greedy Strategy</p><ol><li><p>Check if problem exhibits the optimal substructure property</p></li><li><p>Develop a recursive solution</p></li><li><p>Show that with a greedy choice, only one sub-instance remains</p></li><li><p>Show that the greedy choice is ‘safe’ (exchange argument)</p></li><li><p>Design a recursive greedy algorithm</p></li><li><p>Convert to an iterative algorithm</p></li></ol><p></p>
20
New cards

Ways of representing a graph (digitally)

knowt flashcard image
21
New cards

Degree of a vertex

The number of connections that a vertex has

<p>The number of connections that a vertex has</p>
22
New cards

Depth First Search

  • Explore edges out of the most recently discovered vertex, backtracking when ‘stuck’

  • Continue until all vertices reachable from the start vertex have been discovered and explored

  • If any undiscovered vertices remain, continue using one of them as a new source

  • White - vertex not yet discovered

  • Grey - vertex discovered but not finished

  • Black - vertex finished (finished processing adjacent vertices

Runtime: O(|V| + |E|)

<ul><li><p>Explore edges out of the most recently discovered vertex, backtracking when ‘stuck’</p></li><li><p>Continue until all vertices reachable from the start vertex have been discovered and explored</p></li><li><p>If any undiscovered vertices remain, continue using one of them as a new source</p></li><li><p>White - vertex not yet discovered</p></li><li><p>Grey - vertex discovered but not finished</p></li><li><p>Black - vertex finished (finished processing adjacent vertices</p></li></ul><p></p><p>Runtime: O(|V| + |E|)</p><p></p>
23
New cards

DFS Edge Classifications

Can classify edges in graph or tree

  • Tree edges (T) - edge of DFS forest

  • Back edges (B) - edge to an ancestor vertex

  • Forward edges - edge to a descendent vertex

  • Cross edges (c) - edge to a different tree (vertices not ancestor/descendent of each other

24
New cards

Topological sorting

  • A topological sorting of a directed acyclic graph is a linear ordering of vertices, such that for each edge (u,v), u appears before v

  • i.e. if vertices are arranged on a horizontal line all edges go left to right

Computing:

  • Call DFS(G) to compute start and finish times for each vertex

  • As a vertex is finished, insert into front of a list

  • When finished return list of vertices

<ul><li><p>A topological sorting of a directed acyclic graph is a linear ordering of vertices, such that for each edge (u,v), u appears before v</p></li><li><p>i.e. if vertices are arranged on a horizontal line all edges go left to right</p></li></ul><p></p><p>Computing:</p><ul><li><p>Call DFS(G) to compute start and finish times for each vertex</p></li><li><p>As a vertex is finished, insert into front of a list</p></li><li><p>When finished return list of vertices</p></li></ul><p></p>
25
New cards

Dijkstra’s Algorithm

knowt flashcard image
26
New cards

Dijkstra’s runtime

knowt flashcard image
27
New cards

Jarník-Prim-Dijkstra (Prim’s algorithm)

  • Only cares about the weight of the last edge

  • In prim’s if a vertex is not in the queue then it is ‘black’

<ul><li><p>Only cares about the weight of the last edge</p></li><li><p>In prim’s if a vertex is not in the queue then it is ‘black’</p></li></ul><p></p>
28
New cards

Kruskal’s MST Algorithm

Runtime: O(|E| x log|V|)

<p>Runtime: O(|E| x log|V|)</p>
29
New cards

Sort Algorithms Summary

knowt flashcard image
30
New cards

Loop Invariant

A condition or statement that remains true before and after each iteration of a loop