SlideShare a Scribd company logo
1 of 44
Download to read offline
Data Structures
Jaehyun Park
CS 97SI
Stanford University
June 29, 2015
Typical Quarter at Stanford
void quarter() {
while(true) { // no break :(
task x = GetNextTask(tasks);
process(x);
// new tasks may enter
}
}
◮ GetNextTask() decides the order of the tasks
2
Deciding the Order of the Tasks
◮ Possible behaviors of GetNextTask():
– Returns the newest task (stack)
– Returns the oldest task (queue)
– Returns the most urgent task (priority queue)
– Returns the easiest task (priority queue)
◮ GetNextTask() should run fast
– We do this by storing the tasks in a clever way
3
Outline
Stack and Queue
Heap and Priority Queue
Union-Find Structure
Binary Search Tree (BST)
Fenwick Tree
Lowest Common Ancestor (LCA)
Stack and Queue 4
Stack
◮ Last in, first out (LIFO)
◮ Supports three constant-time operations
– Push(x): inserts x into the stack
– Pop(): removes the newest item
– Top(): returns the newest item
◮ Very easy to implement using an array
Stack and Queue 5
Stack Implementation
◮ Have a large enough array s[] and a counter k, which starts
at zero
– Push(x): set s[k] = x and increment k by 1
– Pop(): decrement k by 1
– Top(): returns s[k - 1] (error if k is zero)
◮ C++ and Java have implementations of stack
– stack (C++), Stack (Java)
◮ But you should be able to implement it from scratch
Stack and Queue 6
Queue
◮ First in, first out (FIFO)
◮ Supports three constant-time operations
– Enqueue(x): inserts x into the queue
– Dequeue(): removes the oldest item
– Front(): returns the oldest item
◮ Implementation is similar to that of stack
Stack and Queue 7
Queue Implementation
◮ Assume that you know the total number of elements that
enter the queue
– ... which allows you to use an array for implementation
◮ Maintain two indices head and tail
– Dequeue() increments head
– Enqueue() increments tail
– Use the value of tail - head to check emptiness
◮ You can use queue (C++) and Queue (Java)
Stack and Queue 8
Outline
Stack and Queue
Heap and Priority Queue
Union-Find Structure
Binary Search Tree (BST)
Fenwick Tree
Lowest Common Ancestor (LCA)
Heap and Priority Queue 9
Priority Queue
◮ Each element in a PQ has a priority value
◮ Three operations:
– Insert(x, p): inserts x into the PQ, whose priority is p
– RemoveTop(): removes the element with the highest priority
– Top(): returns the element with the highest priority
◮ All operations can be done quickly if implemented using a
heap
◮ priority_queue (C++), PriorityQueue (Java)
Heap and Priority Queue 10
Heap
◮ Complete binary tree with the heap property:
– The value of a node ≥ values of its children
◮ The root node has the maximum value
– Constant-time top() operation
◮ Inserting/removing a node can be done in O(log n) time
without breaking the heap property
– May need rearrangement of some nodes
Heap and Priority Queue 11
Heap Example
Figure from Wikipedia
Heap and Priority Queue 12
Indexing the Nodes
◮ Start from the root, number the nodes 1, 2, . . . from left to
right
◮ Given a node k easy to compute the indices of its parent and
children
– Parent node: ⌊k/2⌋
– Children: 2k, 2k + 1
Heap and Priority Queue 13
Inserting a Node
1. Make a new node in the last level, as far left as possible
– If the last level is full, make a new one
2. If the new node breaks the heap property, swap with its parent
node
– The new node moves up the tree, which may introduce
another conflict
3. Repeat 2 until all conflicts are resolved
◮ Running time = tree height = O(log n)
Heap and Priority Queue 14
Implementation: Node Insertion
◮ Inserting a new node with value v into a heap H
void InsertNode(int v) {
H[++n] = v;
for(int k = n; k > 1; k /= 2) {
if(H[k] > H[k / 2])
swap(H[k], H[k / 2]);
else break;
}
}
Heap and Priority Queue 15
Deleting the Root Node
1. Remove the root, and bring the last node (rightmost node in
the last level) to the root
2. If the root breaks the heap property, look at its children and
swap it with the larger one
– Swapping can introduce another conflict
3. Repeat 2 until all conflicts are resolved
◮ Running time = O(log n)
◮ Exercise: implementation
– Some edge cases to consider
Heap and Priority Queue 16
Outline
Stack and Queue
Heap and Priority Queue
Union-Find Structure
Binary Search Tree (BST)
Fenwick Tree
Lowest Common Ancestor (LCA)
Union-Find Structure 17
Union-Find Structure
◮ Used to store disjoint sets
◮ Can support two types of operations efficiently
– Find(x): returns the “representative” of the set that x belongs
– Union(x, y): merges two sets that contain x and y
◮ Both operations can be done in (essentially) constant time
◮ Super-short implementation!
Union-Find Structure 18
Union-Find Structure
◮ Main idea: represent each set by a rooted tree
– Every node maintains a link to its parent
– A root node is the “representative” of the corresponding set
– Example: two sets {x, y, z} and {a, b, c, d}
Union-Find Structure 19
Implementation Idea
◮ Find(x): follow the links from x until a node points itself
– This can take O(n) time but we will make it faster
◮ Union(x, y): run Find(x) and Find(y) to find
corresponding root nodes and direct one to the other
Union-Find Structure 20
Implementation
◮ We will assume that the links are stored in L[]
int Find(int x) {
while(x != L[x]) x = L[x];
return x;
}
void Union(int x, int y) {
L[Find(x)] = Find(y);
}
Union-Find Structure 21
Path Compression
◮ In a bad case, the trees can become too deep
– ... which slows down future operations
◮ Path compression makes the trees shallower every time
Find() is called
◮ We don’t care how a tree looks like as long as the root stays
the same
– After Find(x) returns the root, backtrack to x and reroute all
the links to the root
Union-Find Structure 22
Path Compression Implementations
int Find(int x) {
if(x == L[x]) return x;
int root = Find(L[x]);
L[x] = root;
return root;
}
int Find(int x) {
return x == L[x] ? x : L[x] = Find(L[x]);
}
Union-Find Structure 23
Outline
Stack and Queue
Heap and Priority Queue
Union-Find Structure
Binary Search Tree (BST)
Fenwick Tree
Lowest Common Ancestor (LCA)
Binary Search Tree (BST) 24
Binary Search Tree (BST)
◮ A binary tree with the following property: for each node ðİŚč,
– value of v ≥ values in v’s left subtree
– value of v ≤ dvalues in v’s right subtree
Figure from Wikipedia
Binary Search Tree (BST) 25
What BSTs can do
◮ Supports three operations
– Insert(x): inserts a node with value x
– Delete(x): deletes a node with value x, if there is any
– Find(x): returns the node with value x, if there is any
◮ Many extensions are possible
– Count(x): counts the number of nodes with value less than or
equal to x
– GetNext(x): returns the smallest node with value ≥ x
Binary Search Tree (BST) 26
BSTs in Programming Contests
◮ Simple implementation cannot guarantee efficiency
– In worst case, tree height becomes n (which makes BST
useless)
– Guaranteeing O(log n) running time per operation requires
balancing of the tree (hard to implement)
– We will skip the implementation details
◮ Use the standard library implementations
– set, map (C++)
– TreeSet, TreeMap (Java)
Binary Search Tree (BST) 27
Outline
Stack and Queue
Heap and Priority Queue
Union-Find Structure
Binary Search Tree (BST)
Fenwick Tree
Lowest Common Ancestor (LCA)
Fenwick Tree 28
Fenwick Tree
◮ A variant of segment trees
◮ Supports very useful interval operations
– Set(k, x): sets the value of kth item equal to x
– Sum(k): computes the sum of items 1, . . . , k (prefix sum)
◮ Note: sum of items i, . . . , j = Sum(j) − Sum(i − 1)
◮ Both operations can be done in O(log n) time using O(n)
space
Fenwick Tree 29
Fenwick Tree Structure
◮ Full binary tree with at least n leaf nodes
– We will use n = 8 for our example
◮ kth leaf node stores the value of item k
◮ Each internal node stores the sum of values of its children
– e.g., Red node stores item[5] + item[6]
Fenwick Tree 30
Summing Consecutive Values
◮ Main idea: choose the minimal set of nodes whose sum gives
the desired value
◮ We will see that
– at most 1 node is chosen at each level so that the total
number of nodes we look at is log2 n
– and this can be done in O(log n) time
◮ Let’s start with some examples
Fenwick Tree 31
Summing: Example 1
◮ Sum(7) = sum of the values of gold-colored nodes
Fenwick Tree 32
Summing: Example 2
◮ Sum(8)
Fenwick Tree 33
Summing: Example 3
◮ Sum(6)
Fenwick Tree 34
Summing: Example 4
◮ Sum(3)
Fenwick Tree 35
Computing Prefix Sums
◮ Say we want to compute Sum(k)
◮ Maintain a pointer P which initially points at leaf k
◮ Climb the tree using the following procedure:
– If P is pointing to a left child of some node:
◮ Add the value of P
◮ Set P to the parent node of P’s left neighbor
◮ If P has no left neighbor, terminate
– Otherwise:
◮ Set P to the parent node of P
◮ Use an array to implement (review the heap section)
Fenwick Tree 36
Updating a Value
◮ Say we want to do Set(k, x) (set the value of leaf k as x)
◮ This part is a lot easier
◮ Only the values of leaf k and its ancestors change
1. Start at leaf k, change its value to x
2. Go to its parent, and recompute its value
3. Repeat 2 until the root
Fenwick Tree 37
Extension
◮ Make the Sum() function work for any interval
– ... not just ones that start from item 1
◮ Can support more operations with the new Sum() function
– Min(i, j): Minimum element among items i, . . . , j
– Max(i, j): Maximum element among items i, . . . , j
Fenwick Tree 38
Outline
Stack and Queue
Heap and Priority Queue
Union-Find Structure
Binary Search Tree (BST)
Fenwick Tree
Lowest Common Ancestor (LCA)
Lowest Common Ancestor (LCA) 39
Lowest Common Ancestor (LCA)
◮ Input: a rooted tree and a bunch of node pairs
◮ Output: lowest (deepest) common ancestors of the given pairs
of nodes
◮ Goal: preprocessing the tree in O(n log n) time in order to
answer each LCA query in O(log n) time
Lowest Common Ancestor (LCA) 40
Preprocessing
◮ Each node stores its depth, as well as the links to every 2kth
ancestor
– O(log n) additional storage per node
– We will use Anc[x][k] to denote the 2k
th ancestor of node x
◮ Computing Anc[x][k]:
– Anc[x][0] = x’s parent
– Anc[x][k] = Anc[ Anc[x][k-1] ][ k-1 ]
Lowest Common Ancestor (LCA) 41
Answering a Query
◮ Given two node indices x and y
– Without loss of generality, assume depth(x) ≤ depth(y)
◮ Maintain two pointers p and q, initially pointing at x and y
◮ If depth(p) < depth(q), bring q to the same depth as p
– using Anc that we computed before
◮ Now we will assume that depth(p) = depth(q)
Lowest Common Ancestor (LCA) 42
Answering a Query
◮ If p and q are the same, return p
◮ Otherwise, initialize k as ⌈log2 n⌉ and repeat:
– If k is 0, return p’s parent node
– If Anc[p][k] is undefined, or if Anc[p][k] and Anc[q][k]
point to the same node:
◮ Decrease k by 1
– Otherwise:
◮ Set p = Anc[p][k] and q = Anc[q][k] to bring p and q up
by 2k
levels
Lowest Common Ancestor (LCA) 43
Conclusion
◮ We covered LOTS of stuff today
– Try many small examples with pencil and paper to completely
internalize the material
– Review and solve relevant problems
◮ Discussion and collaboration are strongly recommended!
Lowest Common Ancestor (LCA) 44

More Related Content

Similar to 03-data-structures.pdf

Heaps
HeapsHeaps
HeapsIIUM
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfssuser034ce1
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortReetesh Gupta
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearlESUG
 
designanalysisalgorithm_unit-v-part2.pptx
designanalysisalgorithm_unit-v-part2.pptxdesignanalysisalgorithm_unit-v-part2.pptx
designanalysisalgorithm_unit-v-part2.pptxarifimad15
 
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...Frank Nielsen
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queuesSSE_AndyLi
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 

Similar to 03-data-structures.pdf (20)

DA_02_algorithms.pptx
DA_02_algorithms.pptxDA_02_algorithms.pptx
DA_02_algorithms.pptx
 
Heaps
HeapsHeaps
Heaps
 
Soft Heaps
Soft HeapsSoft Heaps
Soft Heaps
 
Sorting
SortingSorting
Sorting
 
05-stack_queue.ppt
05-stack_queue.ppt05-stack_queue.ppt
05-stack_queue.ppt
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
Q
QQ
Q
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearl
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
s11_bin_search_trees.ppt
s11_bin_search_trees.ppts11_bin_search_trees.ppt
s11_bin_search_trees.ppt
 
designanalysisalgorithm_unit-v-part2.pptx
designanalysisalgorithm_unit-v-part2.pptxdesignanalysisalgorithm_unit-v-part2.pptx
designanalysisalgorithm_unit-v-part2.pptx
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 9) A Concise and Practical Introduction to Programming Algorithms in...
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 

Recently uploaded

DAA Assignment Solution.pdf is the best1
DAA Assignment Solution.pdf is the best1DAA Assignment Solution.pdf is the best1
DAA Assignment Solution.pdf is the best1sinhaabhiyanshu
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...ThinkInnovation
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证pwgnohujw
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...ThinkInnovation
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024patrickdtherriault
 
Las implicancias del memorándum de entendimiento entre Codelco y SQM según la...
Las implicancias del memorándum de entendimiento entre Codelco y SQM según la...Las implicancias del memorándum de entendimiento entre Codelco y SQM según la...
Las implicancias del memorándum de entendimiento entre Codelco y SQM según la...Voces Mineras
 
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...yulianti213969
 
如何办理澳洲拉筹伯大学毕业证(LaTrobe毕业证书)成绩单原件一模一样
如何办理澳洲拉筹伯大学毕业证(LaTrobe毕业证书)成绩单原件一模一样如何办理澳洲拉筹伯大学毕业证(LaTrobe毕业证书)成绩单原件一模一样
如何办理澳洲拉筹伯大学毕业证(LaTrobe毕业证书)成绩单原件一模一样wsppdmt
 
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATIONCapstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATIONLakpaYanziSherpa
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNKTimothy Spann
 
jll-asia-pacific-capital-tracker-1q24.pdf
jll-asia-pacific-capital-tracker-1q24.pdfjll-asia-pacific-capital-tracker-1q24.pdf
jll-asia-pacific-capital-tracker-1q24.pdfjaytendertech
 
Predictive Precipitation: Advanced Rain Forecasting Techniques
Predictive Precipitation: Advanced Rain Forecasting TechniquesPredictive Precipitation: Advanced Rain Forecasting Techniques
Predictive Precipitation: Advanced Rain Forecasting TechniquesBoston Institute of Analytics
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRajesh Mondal
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxParas Gupta
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareGraham Ware
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxronsairoathenadugay
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabiaahmedjiabur940
 
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeBoston Institute of Analytics
 

Recently uploaded (20)

Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
DAA Assignment Solution.pdf is the best1
DAA Assignment Solution.pdf is the best1DAA Assignment Solution.pdf is the best1
DAA Assignment Solution.pdf is the best1
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
 
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotecAbortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024
 
Las implicancias del memorándum de entendimiento entre Codelco y SQM según la...
Las implicancias del memorándum de entendimiento entre Codelco y SQM según la...Las implicancias del memorándum de entendimiento entre Codelco y SQM según la...
Las implicancias del memorándum de entendimiento entre Codelco y SQM según la...
 
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
 
如何办理澳洲拉筹伯大学毕业证(LaTrobe毕业证书)成绩单原件一模一样
如何办理澳洲拉筹伯大学毕业证(LaTrobe毕业证书)成绩单原件一模一样如何办理澳洲拉筹伯大学毕业证(LaTrobe毕业证书)成绩单原件一模一样
如何办理澳洲拉筹伯大学毕业证(LaTrobe毕业证书)成绩单原件一模一样
 
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATIONCapstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
jll-asia-pacific-capital-tracker-1q24.pdf
jll-asia-pacific-capital-tracker-1q24.pdfjll-asia-pacific-capital-tracker-1q24.pdf
jll-asia-pacific-capital-tracker-1q24.pdf
 
Predictive Precipitation: Advanced Rain Forecasting Techniques
Predictive Precipitation: Advanced Rain Forecasting TechniquesPredictive Precipitation: Advanced Rain Forecasting Techniques
Predictive Precipitation: Advanced Rain Forecasting Techniques
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptx
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
 

03-data-structures.pdf

  • 1. Data Structures Jaehyun Park CS 97SI Stanford University June 29, 2015
  • 2. Typical Quarter at Stanford void quarter() { while(true) { // no break :( task x = GetNextTask(tasks); process(x); // new tasks may enter } } ◮ GetNextTask() decides the order of the tasks 2
  • 3. Deciding the Order of the Tasks ◮ Possible behaviors of GetNextTask(): – Returns the newest task (stack) – Returns the oldest task (queue) – Returns the most urgent task (priority queue) – Returns the easiest task (priority queue) ◮ GetNextTask() should run fast – We do this by storing the tasks in a clever way 3
  • 4. Outline Stack and Queue Heap and Priority Queue Union-Find Structure Binary Search Tree (BST) Fenwick Tree Lowest Common Ancestor (LCA) Stack and Queue 4
  • 5. Stack ◮ Last in, first out (LIFO) ◮ Supports three constant-time operations – Push(x): inserts x into the stack – Pop(): removes the newest item – Top(): returns the newest item ◮ Very easy to implement using an array Stack and Queue 5
  • 6. Stack Implementation ◮ Have a large enough array s[] and a counter k, which starts at zero – Push(x): set s[k] = x and increment k by 1 – Pop(): decrement k by 1 – Top(): returns s[k - 1] (error if k is zero) ◮ C++ and Java have implementations of stack – stack (C++), Stack (Java) ◮ But you should be able to implement it from scratch Stack and Queue 6
  • 7. Queue ◮ First in, first out (FIFO) ◮ Supports three constant-time operations – Enqueue(x): inserts x into the queue – Dequeue(): removes the oldest item – Front(): returns the oldest item ◮ Implementation is similar to that of stack Stack and Queue 7
  • 8. Queue Implementation ◮ Assume that you know the total number of elements that enter the queue – ... which allows you to use an array for implementation ◮ Maintain two indices head and tail – Dequeue() increments head – Enqueue() increments tail – Use the value of tail - head to check emptiness ◮ You can use queue (C++) and Queue (Java) Stack and Queue 8
  • 9. Outline Stack and Queue Heap and Priority Queue Union-Find Structure Binary Search Tree (BST) Fenwick Tree Lowest Common Ancestor (LCA) Heap and Priority Queue 9
  • 10. Priority Queue ◮ Each element in a PQ has a priority value ◮ Three operations: – Insert(x, p): inserts x into the PQ, whose priority is p – RemoveTop(): removes the element with the highest priority – Top(): returns the element with the highest priority ◮ All operations can be done quickly if implemented using a heap ◮ priority_queue (C++), PriorityQueue (Java) Heap and Priority Queue 10
  • 11. Heap ◮ Complete binary tree with the heap property: – The value of a node ≥ values of its children ◮ The root node has the maximum value – Constant-time top() operation ◮ Inserting/removing a node can be done in O(log n) time without breaking the heap property – May need rearrangement of some nodes Heap and Priority Queue 11
  • 12. Heap Example Figure from Wikipedia Heap and Priority Queue 12
  • 13. Indexing the Nodes ◮ Start from the root, number the nodes 1, 2, . . . from left to right ◮ Given a node k easy to compute the indices of its parent and children – Parent node: ⌊k/2⌋ – Children: 2k, 2k + 1 Heap and Priority Queue 13
  • 14. Inserting a Node 1. Make a new node in the last level, as far left as possible – If the last level is full, make a new one 2. If the new node breaks the heap property, swap with its parent node – The new node moves up the tree, which may introduce another conflict 3. Repeat 2 until all conflicts are resolved ◮ Running time = tree height = O(log n) Heap and Priority Queue 14
  • 15. Implementation: Node Insertion ◮ Inserting a new node with value v into a heap H void InsertNode(int v) { H[++n] = v; for(int k = n; k > 1; k /= 2) { if(H[k] > H[k / 2]) swap(H[k], H[k / 2]); else break; } } Heap and Priority Queue 15
  • 16. Deleting the Root Node 1. Remove the root, and bring the last node (rightmost node in the last level) to the root 2. If the root breaks the heap property, look at its children and swap it with the larger one – Swapping can introduce another conflict 3. Repeat 2 until all conflicts are resolved ◮ Running time = O(log n) ◮ Exercise: implementation – Some edge cases to consider Heap and Priority Queue 16
  • 17. Outline Stack and Queue Heap and Priority Queue Union-Find Structure Binary Search Tree (BST) Fenwick Tree Lowest Common Ancestor (LCA) Union-Find Structure 17
  • 18. Union-Find Structure ◮ Used to store disjoint sets ◮ Can support two types of operations efficiently – Find(x): returns the “representative” of the set that x belongs – Union(x, y): merges two sets that contain x and y ◮ Both operations can be done in (essentially) constant time ◮ Super-short implementation! Union-Find Structure 18
  • 19. Union-Find Structure ◮ Main idea: represent each set by a rooted tree – Every node maintains a link to its parent – A root node is the “representative” of the corresponding set – Example: two sets {x, y, z} and {a, b, c, d} Union-Find Structure 19
  • 20. Implementation Idea ◮ Find(x): follow the links from x until a node points itself – This can take O(n) time but we will make it faster ◮ Union(x, y): run Find(x) and Find(y) to find corresponding root nodes and direct one to the other Union-Find Structure 20
  • 21. Implementation ◮ We will assume that the links are stored in L[] int Find(int x) { while(x != L[x]) x = L[x]; return x; } void Union(int x, int y) { L[Find(x)] = Find(y); } Union-Find Structure 21
  • 22. Path Compression ◮ In a bad case, the trees can become too deep – ... which slows down future operations ◮ Path compression makes the trees shallower every time Find() is called ◮ We don’t care how a tree looks like as long as the root stays the same – After Find(x) returns the root, backtrack to x and reroute all the links to the root Union-Find Structure 22
  • 23. Path Compression Implementations int Find(int x) { if(x == L[x]) return x; int root = Find(L[x]); L[x] = root; return root; } int Find(int x) { return x == L[x] ? x : L[x] = Find(L[x]); } Union-Find Structure 23
  • 24. Outline Stack and Queue Heap and Priority Queue Union-Find Structure Binary Search Tree (BST) Fenwick Tree Lowest Common Ancestor (LCA) Binary Search Tree (BST) 24
  • 25. Binary Search Tree (BST) ◮ A binary tree with the following property: for each node ðİŚč, – value of v ≥ values in v’s left subtree – value of v ≤ dvalues in v’s right subtree Figure from Wikipedia Binary Search Tree (BST) 25
  • 26. What BSTs can do ◮ Supports three operations – Insert(x): inserts a node with value x – Delete(x): deletes a node with value x, if there is any – Find(x): returns the node with value x, if there is any ◮ Many extensions are possible – Count(x): counts the number of nodes with value less than or equal to x – GetNext(x): returns the smallest node with value ≥ x Binary Search Tree (BST) 26
  • 27. BSTs in Programming Contests ◮ Simple implementation cannot guarantee efficiency – In worst case, tree height becomes n (which makes BST useless) – Guaranteeing O(log n) running time per operation requires balancing of the tree (hard to implement) – We will skip the implementation details ◮ Use the standard library implementations – set, map (C++) – TreeSet, TreeMap (Java) Binary Search Tree (BST) 27
  • 28. Outline Stack and Queue Heap and Priority Queue Union-Find Structure Binary Search Tree (BST) Fenwick Tree Lowest Common Ancestor (LCA) Fenwick Tree 28
  • 29. Fenwick Tree ◮ A variant of segment trees ◮ Supports very useful interval operations – Set(k, x): sets the value of kth item equal to x – Sum(k): computes the sum of items 1, . . . , k (prefix sum) ◮ Note: sum of items i, . . . , j = Sum(j) − Sum(i − 1) ◮ Both operations can be done in O(log n) time using O(n) space Fenwick Tree 29
  • 30. Fenwick Tree Structure ◮ Full binary tree with at least n leaf nodes – We will use n = 8 for our example ◮ kth leaf node stores the value of item k ◮ Each internal node stores the sum of values of its children – e.g., Red node stores item[5] + item[6] Fenwick Tree 30
  • 31. Summing Consecutive Values ◮ Main idea: choose the minimal set of nodes whose sum gives the desired value ◮ We will see that – at most 1 node is chosen at each level so that the total number of nodes we look at is log2 n – and this can be done in O(log n) time ◮ Let’s start with some examples Fenwick Tree 31
  • 32. Summing: Example 1 ◮ Sum(7) = sum of the values of gold-colored nodes Fenwick Tree 32
  • 33. Summing: Example 2 ◮ Sum(8) Fenwick Tree 33
  • 34. Summing: Example 3 ◮ Sum(6) Fenwick Tree 34
  • 35. Summing: Example 4 ◮ Sum(3) Fenwick Tree 35
  • 36. Computing Prefix Sums ◮ Say we want to compute Sum(k) ◮ Maintain a pointer P which initially points at leaf k ◮ Climb the tree using the following procedure: – If P is pointing to a left child of some node: ◮ Add the value of P ◮ Set P to the parent node of P’s left neighbor ◮ If P has no left neighbor, terminate – Otherwise: ◮ Set P to the parent node of P ◮ Use an array to implement (review the heap section) Fenwick Tree 36
  • 37. Updating a Value ◮ Say we want to do Set(k, x) (set the value of leaf k as x) ◮ This part is a lot easier ◮ Only the values of leaf k and its ancestors change 1. Start at leaf k, change its value to x 2. Go to its parent, and recompute its value 3. Repeat 2 until the root Fenwick Tree 37
  • 38. Extension ◮ Make the Sum() function work for any interval – ... not just ones that start from item 1 ◮ Can support more operations with the new Sum() function – Min(i, j): Minimum element among items i, . . . , j – Max(i, j): Maximum element among items i, . . . , j Fenwick Tree 38
  • 39. Outline Stack and Queue Heap and Priority Queue Union-Find Structure Binary Search Tree (BST) Fenwick Tree Lowest Common Ancestor (LCA) Lowest Common Ancestor (LCA) 39
  • 40. Lowest Common Ancestor (LCA) ◮ Input: a rooted tree and a bunch of node pairs ◮ Output: lowest (deepest) common ancestors of the given pairs of nodes ◮ Goal: preprocessing the tree in O(n log n) time in order to answer each LCA query in O(log n) time Lowest Common Ancestor (LCA) 40
  • 41. Preprocessing ◮ Each node stores its depth, as well as the links to every 2kth ancestor – O(log n) additional storage per node – We will use Anc[x][k] to denote the 2k th ancestor of node x ◮ Computing Anc[x][k]: – Anc[x][0] = x’s parent – Anc[x][k] = Anc[ Anc[x][k-1] ][ k-1 ] Lowest Common Ancestor (LCA) 41
  • 42. Answering a Query ◮ Given two node indices x and y – Without loss of generality, assume depth(x) ≤ depth(y) ◮ Maintain two pointers p and q, initially pointing at x and y ◮ If depth(p) < depth(q), bring q to the same depth as p – using Anc that we computed before ◮ Now we will assume that depth(p) = depth(q) Lowest Common Ancestor (LCA) 42
  • 43. Answering a Query ◮ If p and q are the same, return p ◮ Otherwise, initialize k as ⌈log2 n⌉ and repeat: – If k is 0, return p’s parent node – If Anc[p][k] is undefined, or if Anc[p][k] and Anc[q][k] point to the same node: ◮ Decrease k by 1 – Otherwise: ◮ Set p = Anc[p][k] and q = Anc[q][k] to bring p and q up by 2k levels Lowest Common Ancestor (LCA) 43
  • 44. Conclusion ◮ We covered LOTS of stuff today – Try many small examples with pencil and paper to completely internalize the material – Review and solve relevant problems ◮ Discussion and collaboration are strongly recommended! Lowest Common Ancestor (LCA) 44