init repo
3
Final Project/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
__pycache__
|
||||||
|
venv
|
||||||
|
.idea
|
50
Final Project/IC_calculator.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
import sys
|
||||||
|
from project_helper import read_from_file, Color, ALPHABET
|
||||||
|
|
||||||
|
|
||||||
|
def IC_calculate_helper(char_list):
|
||||||
|
ans = 0.0
|
||||||
|
for char in ALPHABET:
|
||||||
|
num = char_list.count(char)
|
||||||
|
ans += num * (num - 1)
|
||||||
|
|
||||||
|
return ans / (len(char_list) * (len(char_list) - 1))
|
||||||
|
|
||||||
|
|
||||||
|
def IC_calculate(char_list, length):
|
||||||
|
char_lists = [[] for _ in range(length)]
|
||||||
|
for i in range(len(char_list)):
|
||||||
|
char_lists[i % length].append(char_list[i])
|
||||||
|
|
||||||
|
print("IC for length %d is " % length, end="")
|
||||||
|
ans = 0.0
|
||||||
|
for i in range(length):
|
||||||
|
ic = IC_calculate_helper(char_lists[i])
|
||||||
|
print("%.5f" % ic, end=", ")
|
||||||
|
ans += ic
|
||||||
|
print("average is " + Color.BOLD + Color.GREEN + "%.5f" % (
|
||||||
|
ans / length) + Color.END)
|
||||||
|
print("------------------------------------")
|
||||||
|
return ans / length
|
||||||
|
|
||||||
|
|
||||||
|
def get_max_ic(char_list, start, end):
|
||||||
|
ans = (0, 0)
|
||||||
|
for i in range(start, end):
|
||||||
|
ic = IC_calculate(char_list, i)
|
||||||
|
if ic > ans[1]:
|
||||||
|
ans = (i, ic)
|
||||||
|
|
||||||
|
print("The highest probability length is", end=" ")
|
||||||
|
print(Color.RED + Color.BOLD + "%d" % ans[0] + Color.END, end=" ")
|
||||||
|
print("with IC" + Color.RED + Color.BOLD, "%.5f" % ans[1] + Color.END)
|
||||||
|
print("------------------------------------")
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
print("Usage: %s <path to file>" % sys.argv[0])
|
||||||
|
exit(-1)
|
||||||
|
charlist = read_from_file(sys.argv[1])
|
||||||
|
get_max_ic(charlist, 4, 6)
|
50
Final Project/analyze.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
import sys
|
||||||
|
from shift import shift
|
||||||
|
from project_helper import read_from_file, ALPHABET
|
||||||
|
|
||||||
|
KEY = "HOLLY"
|
||||||
|
|
||||||
|
|
||||||
|
def analyze(char_list):
|
||||||
|
dictionary = [{} for _ in range(26)]
|
||||||
|
print(char_list)
|
||||||
|
maximum = ("", 0)
|
||||||
|
for i in range(len(char_list)):
|
||||||
|
index = ord(char_list[i]) - ord('A')
|
||||||
|
string = ""
|
||||||
|
if i == 0:
|
||||||
|
string += "-"
|
||||||
|
else:
|
||||||
|
string += char_list[i-1]
|
||||||
|
string += char_list[i]
|
||||||
|
if i == len(char_list) - 1:
|
||||||
|
string += '-'
|
||||||
|
else:
|
||||||
|
string += char_list[i+1]
|
||||||
|
keypair = dictionary[index].get(string)
|
||||||
|
if keypair is None:
|
||||||
|
num = 1
|
||||||
|
else:
|
||||||
|
num = keypair + 1
|
||||||
|
if num > maximum[1]:
|
||||||
|
maximum = (string, num)
|
||||||
|
dictionary[index][string] = num
|
||||||
|
|
||||||
|
for i in range(26):
|
||||||
|
print(ALPHABET[i] + ":", end=" ")
|
||||||
|
for key, value in dictionary[i].items():
|
||||||
|
for j in range(value):
|
||||||
|
print(key, end=" ")
|
||||||
|
print()
|
||||||
|
print("------------------------------------------")
|
||||||
|
print("Most frequency pattern is", maximum[0])
|
||||||
|
print("It appears %d times" % maximum[1])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
print("Usage: %s <path to file>" % sys.argv[0])
|
||||||
|
exit(-1)
|
||||||
|
charlist = read_from_file(sys.argv[1])
|
||||||
|
shifted = shift(charlist, KEY)
|
||||||
|
analyze(shifted)
|
46
Final Project/decode.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
import sys
|
||||||
|
from project_helper import read_from_file
|
||||||
|
|
||||||
|
|
||||||
|
def decode(ciphertexts, passphrase1, passphrase2):
|
||||||
|
alphabet = ['' for _ in range(26)]
|
||||||
|
|
||||||
|
index = 0
|
||||||
|
for i in range(len(passphrase1)):
|
||||||
|
if passphrase1.find(passphrase1[i]) == i:
|
||||||
|
alphabet[index] = passphrase1[i]
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
length = index
|
||||||
|
|
||||||
|
for i in range(26):
|
||||||
|
if passphrase1.find(chr(ord('A') + i)) == -1:
|
||||||
|
alphabet[index] = chr(ord('A') + i)
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
alphabet1 = ['' for _ in range(26)]
|
||||||
|
for i in range(26):
|
||||||
|
j = i // length
|
||||||
|
k = i % length
|
||||||
|
index = 0
|
||||||
|
for l in range(k):
|
||||||
|
index += (26 // length + 1) if l < 26 % length else (26 // length)
|
||||||
|
index += j
|
||||||
|
alphabet1[index] = alphabet[i]
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
ans = ""
|
||||||
|
for char in ciphertexts:
|
||||||
|
if char == ' ':
|
||||||
|
print(" ", end='')
|
||||||
|
continue
|
||||||
|
ans += alphabet1[(ord(char) - ord(passphrase2[count % len(passphrase2)]) + 26) % 26]
|
||||||
|
count += 1
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
print("Usage: %s <path to file>" % sys.argv[0])
|
||||||
|
exit(-1)
|
||||||
|
print(decode(read_from_file(sys.argv[1]), "HANDSOME", "HOLLY"))
|
62
Final Project/histogram.py
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
import sys
|
||||||
|
from IC_calculator import get_max_ic
|
||||||
|
from project_helper import read_from_file, Color, ALPHABET
|
||||||
|
|
||||||
|
|
||||||
|
def print_histogram(char_list, length):
|
||||||
|
char_lists = [[] for _ in range(length)]
|
||||||
|
ans_lists = [[] for _ in range(length)]
|
||||||
|
for i in range(len(char_list)):
|
||||||
|
char_lists[i % length].append(char_list[i])
|
||||||
|
for i in range(len(char_lists)):
|
||||||
|
for char in ALPHABET:
|
||||||
|
print("|", end="")
|
||||||
|
print(f"{char:>{3}}", end=" ")
|
||||||
|
for char in ALPHABET:
|
||||||
|
print("|", end="")
|
||||||
|
print(f"{char:>{3}}", end=" ")
|
||||||
|
print("|")
|
||||||
|
|
||||||
|
for j in range(26):
|
||||||
|
num = char_lists[i].count(ALPHABET[j])
|
||||||
|
print("|", end="")
|
||||||
|
if num == 0:
|
||||||
|
print(Color.RED + Color.BOLD + '████', end="" + Color.END)
|
||||||
|
ans_lists[i].append(True)
|
||||||
|
else:
|
||||||
|
print(f"{num:>{3}}", end=" ")
|
||||||
|
ans_lists[i].append(False)
|
||||||
|
for j in range(26):
|
||||||
|
num = char_lists[i].count(ALPHABET[j])
|
||||||
|
print("|", end="")
|
||||||
|
if num == 0:
|
||||||
|
print(Color.RED + Color.BOLD + '████', end="" + Color.END)
|
||||||
|
else:
|
||||||
|
print(f"{num:>{3}}", end=" ")
|
||||||
|
print("|")
|
||||||
|
print("------------------------------------")
|
||||||
|
|
||||||
|
for i in range(length):
|
||||||
|
for j in range(i + 1, length):
|
||||||
|
print("Possible shifting between %d and %d" % (i + 1, j + 1))
|
||||||
|
if i == 2 and j == 3:
|
||||||
|
pass
|
||||||
|
for l in range(26):
|
||||||
|
num = 0
|
||||||
|
for k in range(26):
|
||||||
|
if ans_lists[i][k] and ans_lists[j][(k + 26 - l) % 26]:
|
||||||
|
num += 1
|
||||||
|
if num >= 4:
|
||||||
|
print("shift by", Color.RED + Color.BOLD + "%d" % l + Color.END, end=", with ")
|
||||||
|
if num <= 6:
|
||||||
|
print(Color.RED + Color.BOLD + "%d" % num, Color.END + "common voids")
|
||||||
|
else:
|
||||||
|
print(Color.GREEN + Color.BOLD + "%d" % num, Color.END + "common voids")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
print("Usage: %s <path to file>" % sys.argv[0])
|
||||||
|
exit(-1)
|
||||||
|
charlist = read_from_file(sys.argv[1])
|
||||||
|
print_histogram(charlist, get_max_ic(charlist, 3, 8)[0])
|
31
Final Project/project_helper.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
def read_from_file(file):
|
||||||
|
try:
|
||||||
|
f = open(file, 'r')
|
||||||
|
except FileNotFoundError:
|
||||||
|
print("Cannot find file", file)
|
||||||
|
exit(-1)
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
char_list = []
|
||||||
|
for char in content:
|
||||||
|
if char.isalpha():
|
||||||
|
char_list.append(char.upper())
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
return char_list
|
||||||
|
|
||||||
|
|
||||||
|
class Color:
|
||||||
|
PURPLE = '\033[95m'
|
||||||
|
CYAN = '\033[96m'
|
||||||
|
DARKCYAN = '\033[36m'
|
||||||
|
BLUE = '\033[94m'
|
||||||
|
GREEN = '\033[92m'
|
||||||
|
YELLOW = '\033[93m'
|
||||||
|
RED = '\033[91m'
|
||||||
|
BOLD = '\033[1m'
|
||||||
|
UNDERLINE = '\033[4m'
|
||||||
|
END = '\033[0m'
|
||||||
|
|
||||||
|
|
||||||
|
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
7
Final Project/renge.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
UOIUV SLVPW IEVIC XFUAG ALZYN TSYAP POPZZ ELTPM CLWMB UOINI
|
||||||
|
ECAWV PBYCJ EBIVS WWIYY EYIPM YWZYY LBYLV VBPYV VFUFC WWIPG
|
||||||
|
SWIHY LINZY WYILC ALIVV MBIWZ LYVAV RCPYL HLAVD YIFCG IFVWV
|
||||||
|
RCYCI ECYVN MBYVC ALJMV UKIIG UOIAI UKCZL LBIZL HLPVS EZUCI
|
||||||
|
MLZFC KPITV EZIWL YPIZL LBACG EZPYL HLZIZ YYWII JFZYM UOIQP
|
||||||
|
PAVIM VKAJW ILITJ EPPYN PAVIJ XTIWH EZIVC FCNIG SDTXN PBLIM
|
||||||
|
USYIM
|
20
Final Project/shift.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import sys
|
||||||
|
from project_helper import read_from_file
|
||||||
|
|
||||||
|
KEY = "VOLLY"
|
||||||
|
|
||||||
|
|
||||||
|
def shift(char_list, key):
|
||||||
|
ans = ""
|
||||||
|
for i in range(len(char_list)):
|
||||||
|
char = chr((ord(char_list[i]) - ord('A') + 26 - (ord(key[i % len(key)]) - ord('A'))) % 26 + ord('A'))
|
||||||
|
ans += char
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
print("Usage: %s <path to file>" % sys.argv[0])
|
||||||
|
exit(-1)
|
||||||
|
char_list = read_from_file(sys.argv[1])
|
||||||
|
print(shift(char_list, KEY))
|
BIN
HW2/4.2_2_both.png
Normal file
After Width: | Height: | Size: 54 KiB |
BIN
HW2/4.2_2_c.png
Normal file
After Width: | Height: | Size: 55 KiB |
BIN
HW2/4.2_2_d.png
Normal file
After Width: | Height: | Size: 56 KiB |
BIN
HW2/4.2_5.png
Normal file
After Width: | Height: | Size: 62 KiB |
BIN
HW2/4.3_12.png
Normal file
After Width: | Height: | Size: 77 KiB |
BIN
HW2/4.3_2_a.png
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
HW2/4.3_2_b.png
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
HW2/4.3_3.png
Normal file
After Width: | Height: | Size: 89 KiB |
BIN
HW2/4.3_6.png
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
HW2/4.3_8_a.png
Normal file
After Width: | Height: | Size: 63 KiB |
BIN
HW2/4.3_8_b.png
Normal file
After Width: | Height: | Size: 64 KiB |
BIN
HW2/4.3_9.png
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
HW2/4.4_2_a&b.png
Normal file
After Width: | Height: | Size: 35 KiB |
BIN
HW2/4.4_4.png
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
HW2/4.4_8.png
Normal file
After Width: | Height: | Size: 43 KiB |
217
HW2/HW2.md
Normal file
|
@ -0,0 +1,217 @@
|
||||||
|
# HW 2
|
||||||
|
## 4.1
|
||||||
|
- 4)
|
||||||
|
- a) Starting from L:
|
||||||
|
|
||||||
|
| L | a | b | c | d | e | f | g | h | i | j | k | l | m | W |
|
||||||
|
| --- | ---------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- |
|
||||||
|
| 0 | **(7, L)** | (14, L) | (11, L) | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | (14, L) | **(11, L)** | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | (16, c) | (27, c) | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | (23, d) | (27, d) | $\infty$ | (25, h) | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | (27, d) | (41, e) | (25, b) | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | (27, d) | (41, e) | **(25, b)** | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | **(27, d)** | (41, e) | **(25, b)** | $\infty$ | (37, h) | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | **(27, d)** | (34, f) | **(25, b)** | (43, f) | (37, h) | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | **(27, d)** | **(34, f)** | **(25, b)** | (43, f) | (37, h) | (46, g) | $\infty$ | $\infty$ | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | **(27, d)** | **(34, f)** | **(25, b)** | (43, f) | **(37, h)** | (46, g) | $\infty$ | (52, j) | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | **(27, d)** | **(34, f)** | **(25, b)** | **(43, f)** | **(37, h)** | (46, g) | (52, i) | (52, j) | $\infty$ |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | **(27, d)** | **(34, f)** | **(25, b)** | **(43, f)** | **(37, h)** | **(46, g)** | (54, i) | **(52, j)** | (57, k) |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | **(27, d)** | **(34, f)** | **(25, b)** | **(43, f)** | **(37, h)** | **(46, g)** | **(54, i)** | **(52, j)** | (57, k) |
|
||||||
|
| 0 | **(7, L)** | **(14, L)** | **(11, L)** | **(16, c)** | **(23, d)** | **(27, d)** | **(34, f)** | **(25, b)** | **(43, f)** | **(37, h)** | **(46, g)** | **(54, i)** | **(52, j)** | **(57, k)** |
|
||||||
|
|
||||||
|
$L->c->d->f->g->k->W$
|
||||||
|
- b)
|
||||||
|
- Starting from f:
|
||||||
|
do the same thing as above
|
||||||
|
the shortest path from f to L is $f->d->c->L$ with 27 roughness and to W is $f->g->k->W$ with 30 roughness.
|
||||||
|
- Starting from i:
|
||||||
|
do the same thing as above
|
||||||
|
the shortest path from i to L is $i->f->d->c->L$ with 43 roughness and to W is $i->k->W$ with 20 roughness.
|
||||||
|
- combine these together, we can get that $27+20 < 43 + 30$, thus the shortest path is $L->c->d->f->i->k->w$ with total roughness $27+16+20=63$
|
||||||
|
|
||||||
|
|
||||||
|
- 9)
|
||||||
|
For a complete graph with three nodes, say a, b and c. The distance are 1, 2,
|
||||||
|
and -999 seperatly for ab, ac, and bc. start from node a, we find the distance
|
||||||
|
of ab and ac are 1 and 2, so we select b and add it to the visited node set.
|
||||||
|
However, the actual shortest path from a to b is $a -> c -> b$ with total cost -997.
|
||||||
|
|
||||||
|
## 4.2
|
||||||
|
- 2)
|
||||||
|
![4.2_2_Both](./4.2_2_both.png "4.2_2_Both")
|
||||||
|
total cost of 59
|
||||||
|
![4.2_2_C](./4.2_2_c.png "4.2_2_C")
|
||||||
|
total cost of 61
|
||||||
|
![4.2_2_D](./4.2_2_d.png "4.2_2_D")
|
||||||
|
**total cost of 57**
|
||||||
|
- 5)
|
||||||
|
![4.2_5](./4.2_5.png "4.2_5")
|
||||||
|
by times -1 with all of the costs and find the MST.
|
||||||
|
|
||||||
|
## 4.3
|
||||||
|
- 2)
|
||||||
|
- a)
|
||||||
|
![4.3_2_a](./4.3_2_a.png "4.3_2_a")
|
||||||
|
- b)
|
||||||
|
![4.3_2_b](./4.3_2_b.png "4.3_2_b")
|
||||||
|
- 3)
|
||||||
|
![4.3_3](./4.3_3.png "4.3_3")
|
||||||
|
- 6)
|
||||||
|
![4.3_6](./4.3_6.png "4.3_6")
|
||||||
|
- 8)
|
||||||
|
- a)
|
||||||
|
![4.3_8_a](./4.3_8_a.png "4.3_8_a")
|
||||||
|
impossible
|
||||||
|
- b)
|
||||||
|
![4.3_8_b](./4.3_8_b.png "4.3_8_b")
|
||||||
|
possible
|
||||||
|
- 9)
|
||||||
|
![4.3_9](./4.3_9.png "4.3_9")
|
||||||
|
5 different ways.
|
||||||
|
- 12)
|
||||||
|
![4.3_12](./4.3_12.png "4.3_12")
|
||||||
|
- 21)
|
||||||
|
because it start from a, then a's child is marked with a$^+$ and the child of child marked child$^+$ which is exacly as the same as a tree and a is the root.
|
||||||
|
## 4.4
|
||||||
|
- 2)
|
||||||
|
![4.4_2_a&b](./4.4_2_a&b.png "4.4_2_a&b")
|
||||||
|
- 4)
|
||||||
|
![4.4_4](./4.4_4.png "4.4_4")
|
||||||
|
- 5)
|
||||||
|
impossible, at least 1 college want 7 Ph.D.s but there is only 6 universities.
|
||||||
|
- 8)
|
||||||
|
![4.4_8](./4.4_8.png "4.4_8")
|
||||||
|
|
||||||
|
## 4.5
|
||||||
|
- 4)
|
||||||
|
- a)
|
||||||
|
North-West:
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | |
|
||||||
|
| --- | --- | --- | --- | --- |
|
||||||
|
| 1 | 30 | | | 30 |
|
||||||
|
| 2 | 10 | 20 | | 30 |
|
||||||
|
| 3 | | 20 | 10 | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | v1:5 | v2:1 | v3:-7 | |
|
||||||
|
| ----- | ------ | ---- | ----- | --- |
|
||||||
|
| u1:0 | 5 | 2(1) | 0(-7) | 30 |
|
||||||
|
| u2:-4 | 9 | 5 | 0(-3) | 30 |
|
||||||
|
| u3:-7 | 4(12)+ | 8 | 0 | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | |
|
||||||
|
| --- | --- | --- | --- | --- |
|
||||||
|
| 1 | 10 | 20 | | 30 |
|
||||||
|
| 2 | 10 | 20 | | 30 |
|
||||||
|
| 3 | 20 | | 10 | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | v1:5 | v2:2 | v3:1 | |
|
||||||
|
| ----- | ---- | ----- | ---- | --- |
|
||||||
|
| u1:0 | 5 | 2 | 0(1) | 30 |
|
||||||
|
| u2:-4 | 9 | 5(6)+ | 0(5) | 30 |
|
||||||
|
| u3:1 | 4 | 8(1) | 0 | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | |
|
||||||
|
| --- | --- | --- | --- | --- |
|
||||||
|
| 1 | 20 | 10 | | 30 |
|
||||||
|
| 2 | | 30 | | 30 |
|
||||||
|
| 3 | 20 | | 10 | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | v1:5 | v2:2 | v3:1 | |
|
||||||
|
| ----- | ---- | ---- | ----- | --- |
|
||||||
|
| u1:0 | 5 | 2 | 0(1)+ | 30 |
|
||||||
|
| u2:-3 | 9(8) | 5 | 0(4) | 30 |
|
||||||
|
| u3:1 | 4 | 8(1) | 0 | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | |
|
||||||
|
| --- | --- | --- | --- | --- |
|
||||||
|
| 1 | 10 | 10 | 10 | 30 |
|
||||||
|
| 2 | | 30 | | 30 |
|
||||||
|
| 3 | 30 | | | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | v1:5 | v2:2 | v3:0 | |
|
||||||
|
| ----- | ---- | ---- | ----- | --- |
|
||||||
|
| u1:0 | 5 | 2 | 0 | 30 |
|
||||||
|
| u2:-3 | 9(8) | 5 | 0(3)+ | 30 |
|
||||||
|
| u3:1 | 4 | 8(1) | 0(-1) | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | |
|
||||||
|
| --- | --- | --- | --- | --- |
|
||||||
|
| 1 | 10 | 20 | | 30 |
|
||||||
|
| 2 | | 20 | 10 | 30 |
|
||||||
|
| 3 | 30 | | | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
| | v1:5 | v2:2 | v3:-3 | |
|
||||||
|
| ----- | ---- | ---- | ----- | --- |
|
||||||
|
| u1:0 | 5 | 2 | 0(-3) | 30 |
|
||||||
|
| u2:-3 | 9(8) | 5 | 0 | 30 |
|
||||||
|
| u3:1 | 4 | 8(1) | 0(-4) | 30 |
|
||||||
|
| | 40 | 40 | 10 | |
|
||||||
|
|
||||||
|
optimized
|
||||||
|
- 6)
|
||||||
|
- a)
|
||||||
|
North-West:
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | 4 | |
|
||||||
|
| --- | --- | --- | --- | --- | --- |
|
||||||
|
| 1 | 40 | | | | 40 |
|
||||||
|
| 2 | 10 | 10 | 10 | | 30 |
|
||||||
|
| 3 | | | 30 | | 50 |
|
||||||
|
| | 50 | 10 | 40 | 20 | |
|
||||||
|
|
||||||
|
optimize:
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | 4 | |
|
||||||
|
| --- | --- | --- | --- | --- | --- |
|
||||||
|
| 1 | | 10 | 30 | | 40 |
|
||||||
|
| 2 | 20 | | 10 | | 30 |
|
||||||
|
| 3 | 30 | | | 20 | 50 |
|
||||||
|
| | 50 | 10 | 40 | 20 | |
|
||||||
|
|
||||||
|
| | v1:4 | v2:2 | v3:5 | v4:1 | |
|
||||||
|
| ---- | ---- | ---- | ----- | ---- | --- |
|
||||||
|
| u1:0 | 7(4) | 2 | 5 | 0(1) | 40 |
|
||||||
|
| u2:1 | 3 | 5(1) | 4 | 0 | 30 |
|
||||||
|
| u3:1 | 4(3) | 6(1) | 3(4)+ | 0 | 50 |
|
||||||
|
| | 50 | 10 | 40 | 20 | |
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | 4 | |
|
||||||
|
| --- | --- | --- | --- | --- | --- |
|
||||||
|
| 1 | | 10 | 30 | | 40 |
|
||||||
|
| 2 | 30 | | | | 30 |
|
||||||
|
| 3 | 20 | | 10 | 20 | 50 |
|
||||||
|
| | 50 | 10 | 40 | 20 | |
|
||||||
|
|
||||||
|
| | v1:6 | v2:2 | v3:5 | v4:2 | |
|
||||||
|
| ---- | ---- | ----- | ---- | ----- | --- |
|
||||||
|
| u1:0 | 7(4) | 2 | 5 | 0(2)+ | 40 |
|
||||||
|
| u2:3 | 3 | 5(-1) | 4(2) | 0(-1) | 30 |
|
||||||
|
| u3:2 | 4 | 6(0) | 3 | 0 | 50 |
|
||||||
|
| | 50 | 10 | 40 | 20 | |
|
||||||
|
|
||||||
|
| | 1 | 2 | 3 | 4 | |
|
||||||
|
| --- | --- | --- | --- | --- | --- |
|
||||||
|
| 1 | | 10 | 10 | 20 | 40 |
|
||||||
|
| 2 | 30 | | | | 30 |
|
||||||
|
| 3 | 20 | | 30 | | 50 |
|
||||||
|
| | 50 | 10 | 40 | 20 | |
|
||||||
|
|
||||||
|
| | v1:6 | v2:2 | v3:5 | v4:0 | |
|
||||||
|
| ---- | ---- | ----- | ---- | ----- | --- |
|
||||||
|
| u1:0 | 7(6) | 2 | 5 | 0 | 40 |
|
||||||
|
| u2:3 | 3 | 5(-1) | 4(2) | 0(-3) | 30 |
|
||||||
|
| u3:2 | 4 | 6(0) | 3 | 0(-2) | 50 |
|
||||||
|
| | 50 | 10 | 40 | 20 | |
|
||||||
|
|
||||||
|
optimized
|
43
HW3/HW3.md
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
# HW3
|
||||||
|
## 10.1
|
||||||
|
- 1.
|
||||||
|
- a) a, c or b, d
|
||||||
|
- b) f
|
||||||
|
- 3.
|
||||||
|
|
||||||
|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | ... |
|
||||||
|
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
||||||
|
| 1 | 1 | 0 | 0 | 1 | 1 | 2 | 2 | 0 | 1 | 0 | 0 | 2 | 1 | 1 | 0 | 0 | 2 | 1 | 1 | 0 | 2 | 2 | 1 | 0 | 0 | 0 | 2 | 1 | 1 | 0 | 0 | 2 | 1 | 3 | 0 | 2 | 2 | 1 | 1 | 0 |
|
||||||
|
|
||||||
|
3, 4, 9, 11, 12, 16, 17, 21, 25, 26, 27, 31, 32, 36, 40+
|
||||||
|
- 8.
|
||||||
|
- a)
|
||||||
|
|
||||||
|
| a | b | c | d |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| 0 | 1 | 0 | 1 |
|
||||||
|
- b) impossible. Since there is no successor for f, then f must be 0. Because the graph is symmetric, so assuming start from e and e is 1, then d is 2, c is 1, b is 2, a is 1, e is 2, thus contradiction.
|
||||||
|
|
||||||
|
- 12.
|
||||||
|
- a) Since $K_n = K_{n−1} ∪ \{x | l(x) = n\ and\ s(x) ∩ K_{n−1} = Ø\}$, $l(x) = n$ iff there exist a path P $X_n->X_{n-1}->X_{n-2}...->X_0$ where $X_n \in L(n)$. This is the longest because if we the previous vertex must have higher level than the successor, if we skip any of the vertex say from $X_n->X_{n-2}$, the total length of the path must be shorter than the longest path gave before.
|
||||||
|
- 15.
|
||||||
|
- Grundy function: value is the first non used non negative integer of value of successors, then it must be different then successor's, proved.
|
||||||
|
- Level numbers: $l(x) = k \Leftrightarrow x \notin L_{k-1}\ and\ s(x) \subseteq L_{k-1}$, thus any vertex must have different level than its successor.
|
||||||
|
|
||||||
|
## 10.2
|
||||||
|
- 1.
|
||||||
|
- a) 10, 11, 11, 101 -> 111, change 101 to 10 (move 3 from 4th)
|
||||||
|
- b) 1, 11, 101, 111 -> 0, no winning condition
|
||||||
|
- c) 10, 100, 100, 110 -> 100, change 100 to 0 or 101 to 1 (move 4 from 2nd, 3rd or 4th)
|
||||||
|
- 2.
|
||||||
|
- a) 10, 0, 0, 10 -> 0, no winning condition
|
||||||
|
- b) 1, 0, 10, 1 -> 10, change 10 to 0 or 0 to 10 (move 2 from 3rd or 1 from 2nd)
|
||||||
|
- c) 10, 1, 1, 0 -> 10, change 10 to 0 or 0 to 10 (move 2 from 1st or 1 from 4th)
|
||||||
|
- 3.
|
||||||
|
- a) 0, 0, 11, 0 -> 11, change 11 to 0 or 0 to 11 (move 3 from 3rd or 2 from 4th)
|
||||||
|
- b) 1, 0, 1, 10 -> 10, change 10 to 0 or 1 to 11 (move 2 from 3rd or 4th)
|
||||||
|
- c) 0, 1, 0, 1 -> 0, no winning condition
|
||||||
|
- 4. Grundy value for number of sticks less than or equal to 7 is the same as exercise 2. Thus the answers are the same except for b), add a winning strategy by moving 5 from 3rd.
|
||||||
|
- 6.
|
||||||
|
- a) 100 to 1 or 110 to 11, move 3 from 2nd, 3rd or 4th
|
||||||
|
- b) 100 to 10 or 110 to 0, move 2 from 2nd or 3rd or all from 4th
|
15
HW4/HW4.md
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# Chapter 1
|
||||||
|
- 15. Since G is a simple graph, then for each vertex, it can have at most 1 edge with every other vetex, so the highest degree for a vertex in a simple graph is (n-1). Also, If there is a vertex with (n-1) degrees, then all the other vertices must have at least 1 degree, which means there are at most 1->(n-1) or 0->(n-2) with total of n-1 possible degrees. However, since there are n vertices, due to the pigeonhole principle, there must be at least two vetexes with the same degree.
|
||||||
|
- 16. 5-gons, it is easy to find a circut with 5 nodes such as the star inside the left graph. There is also circuit with 6 nodes such as the outer loop of the right graph.
|
||||||
|
- 29. There are n vertices and each vertex with degree r, then there is total of $n*r$ degrees. The number of edges is equal to half of the number of degrees, then there is exact $\frac{n*r}{2}$ edges for graph G.
|
||||||
|
- 31. 1. k5,5
|
||||||
|
2. cube
|
||||||
|
3. w4
|
||||||
|
4. impossible, according to q29, n*r must be division by 2, but it is no the case here.
|
||||||
|
5. k7 with a circuit of length 7 removed
|
||||||
|
# Chapter 2
|
||||||
|
- 3. 3, 4, 8, 3, skipped, 5, if regular, 5, if not, 3
|
||||||
|
- 5. assuming a graph is disconnected, then it must be able to divided into two sets of vetices V1 and V2 where there is no edge between V1 and V2. Then its complement graph must contain a sub grapgh of edges with each vertex in V1 with each vetex in V2, which is $K_{(num\ of\ vertices\ in\ V_1),\ (num\ of\ vertices\ in\ V_2)}$ which is connected
|
||||||
|
- 9. if $d(v, w) >= 2$, then there must be a vertex z on any path between v and w. $d(v, z)+d(z,w)$ is not greater than $d(v, w)$ because z can be chosen on the path of $d(v, w)$, also it is not smaller than $d(v, w)$ because if there exists a $d(v, z)+d(z,w)<=d(v, w)$, we can just chose that as the new $d(v, w)$
|
||||||
|
- 33. 1. c5
|
||||||
|
2. two self-complementary graph form a complete regular graph of n nodes and n-1 degrees on each vertex. According to 1.29, $\frac{n*r}{2} = \frac{n*(n-1)}{2}$. Also two self-complementary are isomorphic, then they have same number of edges $\frac{n*(n-1)}{4}$. Either n or n-1 must divisible by 4, n must be 4k or 4k+1.
|
BIN
HW5/14_1.png
Normal file
After Width: | Height: | Size: 113 KiB |
BIN
HW5/14_2.png
Normal file
After Width: | Height: | Size: 108 KiB |
BIN
HW5/19_2.png
Normal file
After Width: | Height: | Size: 61 KiB |
36
HW5/HW5.md
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
# HW5
|
||||||
|
## Chapter 4
|
||||||
|
|
||||||
|
- 5. 2. It cannot contain a $K_5$ because there is only three sets of vertices and each of the sets is an independent set. By ingnoring the connection between, any two of r, s, t can be combined together to form a bipartite graph. Let's set V(n) to be the number of vertices inside a set n, then in order to avoid $K_{3,3}$, the equation below must be satisfied. $MIN(MAX(V(r), V(s), V(t)), (SUM(V(r), V(s), V(t))-MAX(V(r), V(s), V(t))))<3$
|
||||||
|
- 8. 1. $K_5$ with one extra edge between any two vertices.
|
||||||
|
2. It contains a *subgraph* of $K_5$
|
||||||
|
- 10. 1. The only way to draw $K_4$ in planar is bounded by a circle of 3. And the only way to draw $K_{2,3}$ is a circle of even number. Since the outer bound does *NOT* match with the number of nodes, it must not be outerplanar.
|
||||||
|
2. A graph homeomorphic or contradictable to $K_4$ or $K_{2,3}$ is non-outerplanar because we can find a set of nodes and paths between them form a $K_4$ or $K_{2,3}$ by igoring the extra nodes between two nodes. Similarly, for graph contains $K_4$ or $K_{2,3}$, we can ignore the extra nodes/edges that is not belong to $K_4$ or $K_{2,3}$. Thus, we can get a graph of $K_4$ or $K_{2,3}$. As we proved that $K_4$ or $K_{2,3}$ is non-outerplanar, the graph contains a subgraph of homeomorphic or contradictable to $K_4$ or $K_{2,3}$ is non-outerplanar.
|
||||||
|
- 13. 1.
|
||||||
|
| Graph | n | m | f |
|
||||||
|
| :-------: | :---: | :---: | :---: |
|
||||||
|
| $W_8$ | 8 | 14 | 8 |
|
||||||
|
| O | 6 | 12 | 8 |
|
||||||
|
| 4-13 | 9 | 15 | 8 |
|
||||||
|
| $K_{2,7}$ | 9 | 14 | 7 |
|
||||||
|
- 14.
|
||||||
|
i.
|
||||||
|
![14_1](14_1.png "14_1")
|
||||||
|
ii.
|
||||||
|
![14_2](14_2.png "14_2")
|
||||||
|
- 15. 1. see b
|
||||||
|
2. for a planar graph with girth x, since each face is bounded by a circle, so the number of edges of each face must be at least x. Thus we can get these formula: $n-m+f=2$ and $f\leq\frac{2m}{x}$
|
||||||
|
combining these together, we can get $n-2=m-f$ -> $n-2\geq\frac{x-2m}{x}$->$\frac{1}{m}\geq\frac{x-2}{x(n-2)}$->$m\leq\frac{x(n-2)}{x-2}$. By replacing x with f, we can get $m\leq\frac{5(n-2)}{3}$, and for Petersen graph, $n=10, m=15$, which violates the equation.
|
||||||
|
- 16. 1. for a polyhedral, set the number of pentagon is x and hexagon is y, $f=x+y, 5x+6y=2m, m\leq3f-6$, then we can get $5x+6y\leq6(x+y)-12$, $x\geq12$
|
||||||
|
2. if there are exactly 3 degree on each vertex, $m=3f-6$, then $x=12$
|
||||||
|
- 17. 1. $m\leq3f-6,f<12,sum(bdy)=2m$->$sum(bdy)\leq6f-12$, and $\frac{sum(bdy)}{f}$ is the higher bound of the lowest boundary. For $f<12$, $\frac{sum(bdy)}{f}<5$
|
||||||
|
2. if that the case, $\frac{sum(bdy)}{f}=5$ so its upper bound is no longer 4. Dodecahedron
|
||||||
|
- 18. 1. $m=3f-6,sum(bdy)=2m$, we can get $sum(bdy)=6f-12$ -> $12=6f-sum(bdy)$. If we look at each face f and the average value 6, if it is bounded by 3 edges, then the different is $6-3=3$, as well as 4 is 2, 5 is 1, 6 is 0... proved.
|
||||||
|
2. $C_5 = 12$
|
||||||
|
3. if not, then LHS is negative but RHS is positive, contradiction
|
||||||
|
- 19. m for a graph with total number of 11 vertices is 55. But because $m\leq3n-6$, G and G* must have 27 or less edges, thus contradiction
|
||||||
|
![19_2](19_2.png "19_2")
|
||||||
|
- 24. 1. obvious, sum(deg) = sum(bdy) = 2*edges
|
||||||
|
2. divide $d_2$ on both side, we can get $2r+2v-2e=2$ which is exactly Euler's formula times 2 on both sides.
|
||||||
|
3. $2d_1+2d_2-d_1d_2>0$ -> $d_1d_2-2d_1-2d_2<0$ -> $d_1d_2-2d_1-2d_2+4<4$ -> $(d_1-2)(d_2-2)<4$
|
||||||
|
4. (3,3),(3,4),(4,3),(3,5),(5,3)
|
BIN
HW6/4-22.png
Normal file
After Width: | Height: | Size: 131 KiB |
BIN
HW6/4-23.png
Normal file
After Width: | Height: | Size: 107 KiB |
BIN
HW6/5-19.png
Normal file
After Width: | Height: | Size: 214 KiB |
31
HW6/HW6.md
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
# HW6
|
||||||
|
## Chapter 4
|
||||||
|
- 22.
|
||||||
|
> ![4-22](4-22.png "4-22")
|
||||||
|
> $n^*=f=f*=n=6, m*=m=10$
|
||||||
|
- 23.
|
||||||
|
> ![4-23](4-23.png "4-23")
|
||||||
|
> $n^*=f=7, f*=n=6, m*=m=11$
|
||||||
|
- 24.
|
||||||
|
Because we place them in 3D, they can inscribed with each other with each vertex of the inner one lies on different surface once and exactly once. Both of them have same m and $n^*=f, f*=n$. tetrahedron has $f=n=4,m=6$, so its dual graph is itself.
|
||||||
|
- 25.
|
||||||
|
A wheel graph is isomorphic to a pyramid. If we roll a n-sides pyramid $\pi$ along x axis and rotate it $\frac{2\pi}{n}$ along y axis and scale it properly, then it should inscribed with itself with each vertex of the inner one lies on different surface once and exactly once. The wheel or say the pyramid is self-dual.
|
||||||
|
## Chapter 5
|
||||||
|
- 1.
|
||||||
|
1. 2, it is a bipartite graph
|
||||||
|
2. 4, it has a subgraph $K_4$ so it is impossible to color it with less than 4 colors. It is a planar graph so it must be able to colored by 4 or less colors. Thus 4.
|
||||||
|
- 4.
|
||||||
|
1. tetrahedron 4($K_4$), cube 2($bipartite$), octahedron 3, icosahedron 4($W_6$), dodecahedron 3.
|
||||||
|
2. 3 colors for r, s, t seperately.
|
||||||
|
- 7.
|
||||||
|
The original formula can be written as $X(G)(n-d)\geq n$. A graph with (n-d) nodes is equal to a graph $G'$ with a vertex x and all other vertices that have no shared edge with x. Because the vertices that share edges with x must be color by a different color, the number of nodes colored by the same color $X(G)$ is bounded by (n-d). Thus $X(G)(n-d)\geq n$.
|
||||||
|
- 8.
|
||||||
|
1. $n-m+f=2, sum(bdy)=sum(deg)=2m$. Since it contains no triangle, $f\leq\frac{2m}{4}$. Conbine these together, we can get $4m-4n+8\leq 2m$, $4n\geq 2m+8$. If all degrees are at least 4, then $4n\leq 2m$, thus contradiction.
|
||||||
|
2. Assuming the statement is true. Because $G$ contains a vertex v with at most 3 degrees. By removing v we can get graph $G'$. $G'$ is 4 colorable because of the statement. And we have 4 colors, so v must be able to be colored with the remaining color. Proved.
|
||||||
|
- 19.
|
||||||
|
1. A cann't be yg because it is next to yg. If it is blue, then the one on the right of A must be r because it is next to bgy. Then three countries around the center blue cannot be colored b or r, and all of them must have different color, contradiction. A must be red.
|
||||||
|
2. ![5-19](5-19.png "5-19")
|
||||||
|
- 21. a square
|
||||||
|
- 22. The graph is devided by straight lines, then it must have even degree on each vertices and must be Eulerian graph. Thus it must be 2 colorable.
|
||||||
|
- 24.
|
||||||
|
1&2. Dualize the graph, we can get -- By 4-17, there must be at least one vertex with degree less than or equal to 4. Assuming the statement is true, then the graph $G'$ by removing one vertex v with less than or equal to 4 degrees must be 4 colorable. If the neighbors of v use less than 4 colors, then it is 4 colorable. If they use all, then mark 4 vertices around v as $v_a, v_b, v_c$ and $v_d$ clockwise, and their color are a, b, c, d seperately. If there is not a path between $v_a$ and $v_c$ with nodes that only colored by a or c, then for any path from $v_a$ and only colored by a or c, reverse their color. Now v can be colored by a. If there is a path like that, then there must not be a path like that between b and d, do the same thing then the graph is 4 colorable.
|
BIN
HW7/5-25.png
Normal file
After Width: | Height: | Size: 17 KiB |
18
HW7/HW7.md
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# HW7
|
||||||
|
## Chapter 5
|
||||||
|
|
||||||
|
- 9.
|
||||||
|
- 1. $k(k-1)(k-2)(k-3)(k-4)(k-5)$ 7!
|
||||||
|
- 2. $k(k-1)^5$ 7*6^5
|
||||||
|
- 12.
|
||||||
|
- 1. $k(k-1)^5+k(k-1)(k-2)^5$
|
||||||
|
- 2. $k(k-1)^4-(k(k-1)^3-k(k-1)(k-2))$
|
||||||
|
- 25. ![5-25](5-25.png "5-25")
|
||||||
|
- 28.
|
||||||
|
| Graph | Lower | Upper | Actual |
|
||||||
|
| :-------: | :---: | :---: | :----: |
|
||||||
|
| $C_5$ | 2 | 3 | 3 |
|
||||||
|
| $K_8$ | 7 | 8 | 7 |
|
||||||
|
| $K_{4,6}$ | 6 | 7 | 6 |
|
||||||
|
- 31. Degree = 3, Color>=3
|
||||||
|
Form a Hamiltonian path, color it with 2 colors(total vertices must be even(2m = sum(deg))), and the remaining with the 3rd color
|