5.4: The degree is the # of neighbours. (this is for average case) For list: We go to the node i’s adj list and traverse through, in the worst case scenario, nodes, while summing. So O(N) For adj matrix: You also have to iterate but through all entries in the row. So also O(N)
5.5. in-degree(i)) is the degree of nodes pointing into a node
For adj matrix, this simply as you summing adj_mat[j][i] where (increment in for loop).
- To be clear, i mean we’re summing the column that represents. The row is variable; the column (for ) is constant.
- This is simply
For adj list, you have to go thru EACH list, and for EACH traverse until you hit and increment. That will be
5.6
for i in N
for j in N
for -1 <= x-dir < 1
for -1 <= y-dir < 1
attemp to move x,y dir. otherwise don't
if (mouse at cheese) we're done!
Wow that sucks
5.8: This will suck. Is this kinda right? I shoulda used a while loop instead.
Node *temp_before;
Node *temp;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (temp->left == NULL) break;
temp = temp->left;
}
printf(temp.data);
print(temp_before.data)
temp = temp_before->right;
}
5.9: Basically that one mars exercise we did
5.10 A3 had us implement this.

-
values
- 22
-
values
- 289
-
values
- 3
-
ptrs
- &2
-
ptrs
- 2211 (size 5 array)
-
ptrs (added by line 12)
- 0
-
return
line 12 is WACK. Let’s assume it doesn’t crash the program. line 15 would do something far away. could defo crash the program tho ngl
Things I learned
- replacing a bit of memory w/ something you created does not make you own it, but you can still “change” it and access it so long as you don’t own the data.
- Line 12 can work but sometimes a program owns that data and you’ll be in for a treat when you get a segfault!
void modify (Object* head, double position[2], double Colour[3], int depth, int type)
{
if (head == NULL) return;
head->position[0] = position[0];
etc.
head->color[0] = Colour[0]
etc.
head->depth = fiwahifwhipafhipwa
u get the idea
}Object* removeDups(Object* head)
{
if (head == NULL) return head;
Object *traverse = head;
Object *compare = head;
while (compare->next != NULL)
{
while (traverse->next != NULL)
{
if (compare != traverse && checkForDupe(compare, traverse) == 1)
}
}
}- If all N items are not dupes, we’d have to cycle through N nodes to check, as well as N nodes to compare the node we’re checking with.
- Assuming these lists are not sorted, we’d have to compare all N songs with all N songs in the other list. so n^2?
- By implementing a BST search, we can find a item is in a BST in log(2)
- oops sorry i said avg / best case
- For worst case, the two BSTs are just linked lists. so the same thing.
- todo READ THE QUESITON CAREFULLY!
- wack
15
- C = not exclusive but related
- Go through each medicine that can be taken with A
- Compare it with every possible substitute of B
- If the medicine matches a substitute, we found our C
As for complexity, assuming all but 1 drug is able to be taken with A and substitutes B, you would need to go through N-1 drugs with N comparisons until finding the right one. That gives
Going through all N possible drugs takes N steps. For the ones that are non-exclusive, we can throw them into the matrix and see if they are a substitute of B at a constant O(1) speed. Ans: O(N)
17 A tail recursive function, with a compiler that can notice the pattern, can optimize memory and overhead of dealing with the function call stack. It occurs if you return just the recursive call with no other operations done around it.