Searching

The unit of work here is… ?todo

FunctionAvg. Big OW.C. Big ONotes
Array: Linear search--
Array: Binary Search-Assumes array is sorted
Linked List: Linear Search-Similar reasoning to linear search for arrays
BST: “Search”
If data items are added in order to a BST, it will resemble a linked list and therefore will not have the advantages of a BST

Sorting

For these, we usually count the number of “comparisons” made.

FunctionAvg. Big OW.C. Big ONotes
Array: Bubble Sort
-The number of comparisons is
Array: Merge Sort
-Discussed in detail elsewheretodo
Linked List: Bubble Sort
-Swapping: Constant
Going to next node: Constant
Going through each node: N
Traversing through the N-1 nodes to find a smaller one: N

Linked List: Merge Sort
-”Traversing to the center node is not contribute to the complexity”
As we’re only counting comparisons.
Creation of BST
(“Tree sort”)
Creating a BST from an array sorts it for you!
Note that the worst case comes if every item in an array is already sorted; in that case you don’t have the benefit of a BST

Linked List Operations

Unit of work: Accessing a pointer (?)

FunctionAvg. Big OW.C. Big ONotes
Insert (at head)--
Insert (at tail)--
Delete--

BST Operations

Unit of work: Accessing a pointer (?)

FunctionAvg. Big OW.C. Big ONotes
InsertEquivalent to “finding” a node (whose complexity we discussed above). The extra step of “adding” the node is a constant amount of work
DeleteSame as above.

Operations on graphs

Graph Operations (Adjacency List) w/ lists stored in an array

Note: the edge lists are stored in an array.

Also note: these apply for directed OR un-directed graphs. The only difference is that for the directed graphs you’d have to double the work (updating ‘s relation to and ‘s relation to )

Also ALSO note: for the “removing node” complexities, we do NOT include updating the array as that will mess up all the indices!

FunctionAvg. Big OW.C. Big ONotes
Adding Edge (insert at head)O(1)-Inserting at tail will be
Removing Edge-We traverse on average nodes before finding the one to delete.

As Paco phrases it: for the node to delete . The array entry can be connected to at most nodes, so that is what we traverse in the worst case.
Adding Node-We have to duplicate the array that holds the pointers to all the linked lists
Removing NodeFor avg case: We go through every edge and, if it’s an edge relating to the deleted node, we remove it.

For worst case, there will be edges for all nodes.
Going through all nodes: N
For each N node, traversing to locate the edge to delete:
Edge Query-Accessing node: (array indexing)
Finding edge:

Graph Operations (Adjacency Matrix)

FunctionAvg. Big OW.C. Big ONotes
Adding Edge-Accessing an entry in a matrix is constant work
Removing Edge-Same as abv
Adding Node-Rebuilding a matrix is work
Removing Node-Going thru each entry in a row and column to 0 it:
Edge Query-Same as the top 2

Extra: Graph Operations (Adjacency List) w/ lists stored in a linked list

FunctionAvg. Big OW.C. Big ONotes
Adding Edge (insert at head)-Traverse to the pointer:
Add edge: (const)
Removing Edge-Traverse to pointer + traverse to edge:
Adding Node-We can insert the new node at the head of the linked list
Removing Node-Traverse to the node, then free the adj list
Edge Query-Traverse to pointer + traverse to edge: