1/26
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a Heap (2 answers)
A special tree-based data structure where the tree is a complete (or almost complete) binary tree.
It is a minimum height binary tree where the deepest level is filled from left to right.
Min Heap
When a smaller value has a higher priority than a larger one
Max Heap
When a larger value has a higher priority than a smaller one
Does a heap order care about the relative order of the siblings
No, it is only concerned with the value of its descendants.
Time complexity for Heap insertion
O(log(n))
What is the best strength/utility of a Heap
That the smallest or largest value can be found in O(1) time.
Insert method steps (Heap)
Insert value into heap
Perform upheap
Increment next
counter
Upheap Steps
Compare the inserted node with its parent.
If node is smaller than parent, swap, continue up.
If parent is smaller than node, stop.
Delete method steps (Heap)
If root has no children, delete.
Swap root with last leaf, delete.
Perform Downheap.
Downheap Steps
Compare parent, left, and right
If parent is smallest then stop
if left is smallest then swap with parent and continue left.
if right is smallest then swap with parent and continue right.
For a given node i in a heap, what are its left, right, and parent
Its Left Child is at position 2 * i
Its Right Child is at position 2 * i + 1
Its Parent is at position int i/2 (aka the floor)
Graph Concept
A set of nodes (vertices) and a set of edges (arcs) used to model abstracted versions of real-life systems.
Common Graph use cases
Relationship data
Location data
Network data
Graph Paths Concept
A path is an ordered collection of node-edge transitions.
Graph types
Undirected
Directed
Cyclic
Acyclic
Weighted
Unweighted
Sparse
Dense
Directed/Undirected Graphs
A graphs edges can optionally have a direction (i.e. a directed graph).
If there is a direction, movement must follow that direction.
If an edge has no direction, movement can go both ways.
Weighted/Unweighted
An edge can have a weight (cost) (i.e. a weighted graph).
The transition from one node to another costs that weight.
This could represent the distance, or time, or anything you want to represent.
Edges without weights are unweighted
Cyclic/Acyclic
Cycles begin at the same node and contain at least one edge.
A graph without cycles is called Acyclic
Sparse/Dense
Dense is where there are many edges(arcs) present.
Sparse is when there are edges (arcs) missing.
Graph Traversal Methods (2 answers)
Depth-First Traversal
Breadth-First Traversal
Depth-First Traversal Utility
Finding cycles
Finding valid paths
Identifying connected nodes
Can travel very deep into the graph
Depth-First Traversal Algorithm
Randomly pick a starting node, mark it as visited
Recursively move to an adjacent node, keeping track of visited nodes.
Eventually you will arrive at a node that has no non-visited adjacent nodes.
When you do, backtrack to the previous node.
Breadth-First Traversal Utility
Travels a short distance but finds the shortest path on unweighted graphs
Dijkstra’s algorithm
Finds the shortest path on weighted graphs (no negatives)
Breadth-First Traversal Algorithm
Randomly pick a starting node, mark it as visited
Identify all adjacent nodes.
Recursively visit them, keeping track of visited nodes
Continue until all nodes have been visited
Adjacency Matrix
Good for dense graphs, 1 is for if there is an edge connecting the nodes and 0 is if there is no edge.
Adjacency List
Good for sparse graphs
List with all the nodes
Keeps track of its neighbours.