-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathNOTES.txt
166 lines (110 loc) · 4.08 KB
/
NOTES.txt
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# ------------------------------------------------------------------- #
# #
# Algorithms and Data Structures #
# #
# --------------------------------------------------------------------#
by John Shiver
# --------------------------- Arrays ------------------------------ #
Methods
-
Advantages
- Sorted arrays support binary search and logarithmic query times
Disadvantages
- linear time update
#---------------------------------------------------------------------#
# ------------------------- Linked List ---------------------------- #
Methods
get(): Gets an element
O(n)
add(): Adds element to end of list
O(1)
insert(i): Inserts element and index i
O(n)
delete(): Removes element from list
O(1)
Advantages
- for unsorted doubly-linked lists, insertion and deletion is O(1) time
Disadvantages
- search takes O(n) time in work case
# ------------------------------------------------------------------- #
# --------------------------- Trees ------------------------------- #
a
/ \
/ \
/ \
/ \
b c
/ \ / \
/ \ / \
d e f g
/ \ / \ / \ / \
h i j k l m n o
Methods
get(): Gets element in tree
O(h) -> h is height of the tree
insert(): only one place to insert, the root node
O(h) + O(1) -> insertion takes O(1) after position is found
remove(): removes element in tree
O(h) + O(1)
Advantages
- All three methods are O(h)
Disadvantages
-
Terms:
"rooted binary tree": cosists of node called "root", together with two
rooted binary trees called left and right subtrees
- order among "brother" nodes matters in rooted trees,
so left is different than right
"binary search tree": labels each node in a binary tree with a single key
such that for any node labeled x, all nodes left
subtree of x have keys < x while all nodes right
subtree of x have keys > x.
- for any binary tree on n nodes, and any set
of n keys, there is 'exactly' one labeling that
makes it a binary search tree.
Notes:
- The smallest height we can hope for is when the tree is perfectly balanced
-
#---------------------------------------------------------------------#
# --------------------------- Graphs ------------------------------ #
Notes
What is a graph?
- linked list + arrays organize data sequentially, they are linear
structures. useful for iterating over all elements or accesing
elements via index
- other data structures, such as trees, are hierarchical structures
parent / child, for example.
- graphs represent relationships between objects
Basic objects: vertices, nodes
Relationships between them: edges, arcs, links
Examples:
- Basic object: websites
- Relationship between them: hyperlinks
- Basic object: people
- Relationship between them: friends
What kind of questions can we ask of the graph:
- Are two vertices adjacent?
- Is the graph dense? Sparse?
- How far are two vertices in the graph?
- How many components are there in the graph?
- Can we find a vertex with a particular key value?
Size of graph: |V| + |E| : the sum of vertices and edges
Path: sequence of vertices and edges that depicts hopping along the graph
Representations:
1. Adjacency Matrix
- use a matrix with rows and columns
- row labels and column labels represent vertices
2. Adjacency List
3. Adjacency Set
- subset of adj list representation
The graph interface
public interface Graph {
enum GraphType {
DIRECTED,
UNDIRECTED
}
void addEdge(int v1, int v2);
List<Integer> getAdjacentVertices(int v);
}
# --------------------------------------------------------------------- #