data structures 2 Flashcards
- How do you test for an empty circular queue?
- How do you test for a full circular queue
- Check if the front and rear pointers are equal (front == rear)
- Check if the tail pointer is 1 less than the front pointer
How do you add an item to a priority queue?
- Ensure the queue is not full.
- Place the item according to its priority. Higher priority items are placed before lower priority items.
- Shift existing elements to accommodate the new item while maintaining the order
How do you remove an item from a priority queue?
- Ensure the queue is not empty.
- Take the item with the highest priority (usually the first item).
- Shift remaining elements to fill the gap left by the removed item.
How do you test for an empty priority queue?
Check if the size of the queue is 0 or if the front pointer is equal to the rear pointer.
How do you test for a full priority queue?
Check if the rear pointer is at the last index of the array (size - 1)
uses of graphs
- navigation systems
- data transmissions
- web page links
- social media trends
differences between tree and graph
- graphs have cycles, trees dont
- theres one path between each node in a tree, there are multiple with a graph
what are edges/arcs
the paths that link nodes together
a graph with more edges than vertices is called
a graph with less edges than vertices is called
a dense graph
a sparse graph
directed vs undirected graphs
where the edges go in one direction
undirected - all the edges are biderectional (arrowheads on both heads)
how to represent:
- vertices
- edges
- edges with weights
vertices - inside curly brackets, eg {A,B,C,D,E,F}
edges - {(A,B),(A,C), (B,E), (C,A)}
edges with weights -
{ (A,B,8), (A,C,2), (B,E,4) }
adjacency matrix
- A square 2D matrix used to represent a graph
- Each row and column represent a node (or vertex).
adjacency list
- A list of dictionary where each node (or vertex) has a list of its adjacent nodes (or neighbors).
- implemented as a list of dictionaries
- key in each dictionary being the node and edge weight
advantages and disadvantages of adjacency matrixes
- easy and quick to work with
- adding new edges/ checking for the presence of edges is quicker using this structure
however:
- if theres many nodes and few edges, it may result in empty space (wasted memory) and problem gets worse as size of the graph increases
- if matrix is implemented as a static 2D array, it can be hard to add or delete nodes
advantages of adjacency lists
- more space efficient
- use less memory to store a sparse graph