370 midterm notes
This commit is contained in:
parent
787e8bed86
commit
505c09500c
@ -1,9 +1,11 @@
|
||||
cc=javac
|
||||
fxlib=--module-path /home/shockrah/Downloads/javafx-sdk-11.0.2/lib
|
||||
ctrl=--add-modules javafx.controls
|
||||
|
||||
env=java
|
||||
cfile="adsf"
|
||||
#fxlib=--module-path /home/shockrah/Downloads/javafx-sdk-11.0.2/lib
|
||||
#ctrl=--add-modules javafx.controls
|
||||
|
||||
jfile="Main.java"
|
||||
# Target class file to run(no extension)
|
||||
cfile="Main"
|
||||
|
||||
default: %.java
|
||||
# takes a java file as entry to build
|
||||
|
@ -21,7 +21,12 @@ Time: O(VlogV + E)
|
||||
|
||||
## Dijkstra's
|
||||
|
||||
O(V)
|
||||
O(V^2 + E)
|
||||
|
||||
## A\*
|
||||
O(V^2 = E)
|
||||
|
||||
Worst case is the same as Dijkstra's time
|
||||
|
||||
O(V^2 + E)
|
||||
|
||||
|
||||
|
@ -7,7 +7,7 @@ _time complexity will be in terms of height_
|
||||
* impl: _root will typically have the largest height value_
|
||||
|
||||
> Balance:
|
||||
| left_height - right_height |
|
||||
left_height - right_height
|
||||
we can discard the || if we want negative balance vals but it really shouldn't matter
|
||||
basically: we want the balance for each node to be 1 or 0.
|
||||
|
||||
|
17
370/notes/single-source.md
Normal file
17
370/notes/single-source.md
Normal file
@ -0,0 +1,17 @@
|
||||
# Single Source Shortest Path
|
||||
|
||||
# Bellman-ford Algorithm
|
||||
|
||||
Time: O(VE)
|
||||
|
||||
# Floyd-Warshall
|
||||
|
||||
Space: O(V^2)
|
||||
|
||||
That space is because we're using a matrix to store the paths, which of course is going to take up two dimensions
|
||||
|
||||
Main idea: Shortest path between any two nodes in a graph w/ V nodes _will_ go through at most V nodes
|
||||
|
||||
Iterative idea: Path's can visit i intermediary node. Does that many any path shorter?
|
||||
|
||||
Advantages: we can find negative cycles(Cycle where there is a negative edge)
|
53
370/past-midterms/2.md
Normal file
53
370/past-midterms/2.md
Normal file
@ -0,0 +1,53 @@
|
||||
# Time/space complexity
|
||||
|
||||
For basically everthing, just list it out somewhere on sheet
|
||||
|
||||
# AVL Tree's
|
||||
|
||||
Get some functions for them written out
|
||||
Insert/Deletion/Lookup Functions
|
||||
|
||||
# No pathfinding code
|
||||
_mainly cuz we never did any for homework/class_
|
||||
|
||||
Time/space complexities should be straightforward memorization
|
||||
|
||||
# Tries
|
||||
|
||||
|
||||
# Huffman
|
||||
|
||||
Decoding:
|
||||
: Time - O(n) {n = number of input bits}
|
||||
|
||||
Encoding
|
||||
: Time O(nlog(n))
|
||||
: Where n is the number of unique bits in the string
|
||||
|
||||
|
||||
# Pathfinding - Conceptual things
|
||||
|
||||
Admissability: allowed to underestimate but not overestimate.
|
||||
|
||||
> Heuristic?
|
||||
|
||||
* A-star
|
||||
* Best-first
|
||||
|
||||
> Multiple sources
|
||||
|
||||
* Floyd's Algorithm
|
||||
|
||||
> Multiple destination
|
||||
|
||||
* Floyd's
|
||||
* Bellman-Ford
|
||||
|
||||
> Negative edges
|
||||
|
||||
* Floyd
|
||||
* Bellman-Ford
|
||||
|
||||
> Negative Cycles
|
||||
|
||||
None
|
96
370/past-midterms/2.py
Normal file
96
370/past-midterms/2.py
Normal file
@ -0,0 +1,96 @@
|
||||
class HuffmanNode:
|
||||
# $ will represent a dummy node
|
||||
# anything else is a regular node
|
||||
def __init__(self, data, freq):
|
||||
self.data = data
|
||||
self.freq = freq
|
||||
|
||||
|
||||
class HuffmanTree:
|
||||
def __init__(self, node):
|
||||
self.root = node
|
||||
|
||||
def frequencyMap(self, string):
|
||||
ret = {}
|
||||
for i in string:
|
||||
if i not in ret:
|
||||
ret[i] = Node(i, 1)
|
||||
else:
|
||||
ret[i].freq += 1
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def encode(self, string):
|
||||
f = self.frequencyMap(string)
|
||||
|
||||
class TrieNode:
|
||||
def __init__(self):
|
||||
# 1 ref for each character in the alphabet
|
||||
self.isleaf = False
|
||||
self.edges = [0] * 26
|
||||
|
||||
class TrieTree:
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
|
||||
# just not going to bother with capitals
|
||||
def _charIndex(self, char):
|
||||
return ord(char) - ord('a')
|
||||
|
||||
def insert(self, string):
|
||||
curr = self.root
|
||||
for i in string:
|
||||
alphabetIndex = _charIndex(i)
|
||||
if curr[alphabetIndex] is None:
|
||||
curr.edges[alphabetIndex] = Node()
|
||||
# go to the nexxt thing
|
||||
curr = curr.edges[alphabetIndex]
|
||||
else:
|
||||
# Finally mark the last node as a leaf
|
||||
curr.isleaf = True
|
||||
|
||||
def lookup(self, string):
|
||||
curr = self.root
|
||||
# First we should go straight to the end of the string
|
||||
# That's why we don't do any checks
|
||||
for i in string:
|
||||
alphaIndex = _charIndex(i)
|
||||
curr = curr.edges[alphaIndex]
|
||||
|
||||
return curr.isleaf
|
||||
|
||||
def remove(self, string):
|
||||
pass
|
||||
|
||||
def traverse(self, node, preFix):
|
||||
if node:
|
||||
if n.isleaf:
|
||||
print(prefix)
|
||||
# recursively go through the tree
|
||||
for i in prefix:
|
||||
traverse(node.edges[_i], prefix+_charIndex(i))
|
||||
|
||||
class AVLNode:
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.height = 0
|
||||
self.left = None
|
||||
self.Right = None
|
||||
|
||||
class AVLTree:
|
||||
def __init__(self,node):
|
||||
self.root = node
|
||||
|
||||
def rotateLeft(self, node):
|
||||
tmp = node.right
|
||||
node.left = tmp.right
|
||||
tmp.right = node
|
||||
|
||||
# Set the heights on each node
|
||||
node.height = 1 + max(node.left.height, node.right.height)
|
||||
tmp.height = 1 + max(tmp.left.height, tmp.right.height)
|
||||
return tmp
|
||||
|
||||
def rotateRight(self, node):
|
||||
pass
|
BIN
370/past-midterms/midterm2.pdf
Normal file
BIN
370/past-midterms/midterm2.pdf
Normal file
Binary file not shown.
27
370/past-midterms/tmp.py
Normal file
27
370/past-midterms/tmp.py
Normal file
@ -0,0 +1,27 @@
|
||||
class Trie:
|
||||
def __init__(self, *words):
|
||||
self.root = {}
|
||||
|
||||
for word in words:
|
||||
self.insert(word)
|
||||
|
||||
def insert(self, word):
|
||||
curr = self.root
|
||||
for c in word:
|
||||
if c not in curr:
|
||||
curr[c] = {}
|
||||
else:
|
||||
curr = curr[c]
|
||||
|
||||
# add a marker to show that we are a leaf
|
||||
curr['.'] = '.'
|
||||
self.root = curr
|
||||
|
||||
def remove(self, word):
|
||||
pass
|
||||
|
||||
def printWord(self, word):
|
||||
pass
|
||||
|
||||
def all(self):
|
||||
pass
|
59
370/past-midterms/trie.py
Normal file
59
370/past-midterms/trie.py
Normal file
@ -0,0 +1,59 @@
|
||||
class Node:
|
||||
def __init__(self, leaf=False):
|
||||
# refs list of next valid characters
|
||||
self.letters = [None] * 26
|
||||
self.isLeaf = leaf
|
||||
|
||||
|
||||
class Trie:
|
||||
def __init__(self):
|
||||
self.root = Node()
|
||||
|
||||
def _charIndex(self, char):
|
||||
return ord(char) - ord('a')
|
||||
|
||||
def insert(self, string):
|
||||
curr = self.root
|
||||
for i in string:
|
||||
refIndex = self._charIndex()
|
||||
# create nodes if we have to
|
||||
if curr.letters[refIndex] is None:
|
||||
curr.letters[refIndex] = Node()
|
||||
|
||||
curr = curr.letters[refIndex]
|
||||
# Set the last letter to be a leaf
|
||||
curr.isLeaf = True
|
||||
|
||||
def remove(self, string):
|
||||
# Lazy removal
|
||||
curr = self.root
|
||||
for i in string:
|
||||
refIndex = self._charIndex(i)
|
||||
if curr.letters[refIndex] is None:
|
||||
break
|
||||
curr = curr.letters[refIndex]
|
||||
curr.isLeaf = False
|
||||
|
||||
|
||||
# We're also going to be printing things as we go along the thing
|
||||
def lookup(self, string):
|
||||
curr = self.root
|
||||
for i in string:
|
||||
refIndex = self._charIndex(i)
|
||||
# Make sure we don't walk into nothing
|
||||
if curr.letters[refIndex] is None:
|
||||
return False
|
||||
curr = curr.letters[refIndex]
|
||||
print(i, end='')
|
||||
|
||||
return curr.isLeaf
|
||||
|
||||
def all(self, node, word):
|
||||
if node is None:
|
||||
return
|
||||
|
||||
if node.isLeaf:
|
||||
# Print out the word we have so far
|
||||
self.lookup(word)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user