SlideShare a Scribd company logo
Advanced Algorithms
Exercise 2

Group 2
陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧
2013/11/6

Q1 Q8

1
Q3
• Given an integer (not necessary a decimal
number) with n digits, we want to remove m
(m < n) digits from this number such that the
resulting number is as large as possible.
Design an O(n) time algorithm to solve it.
• Ex. n = 9, m = 3
input : 987645821
remove bold place, maximum result = 987821
2013/11/6

Q1 Q8

2
Q3 – efficient algorithm
s[] : digit as [0, n-1]
r[] : maximum result (stack)
for(i = top = 0; i < n; i++)
if(top + n – i <= n)
while(i < n)
r[top++] = s[i++]
break;
while(top > 0 && s[i] > r[top-1])
top--;
if(top + n – i <= n)
break;
r[top++] = s[i]
return r[];
2013/11/6

Q1 Q8

3
Q4
• Let x1, x2, … , xn be a sequence of integers
and k be another given integer. Design an
algorithm to find a subsequence xi , xi+1, …,
xj (of consecutive elements) such that the sum
of the numbers in it is exactly k if there does
exist such a subsequence.

2013/11/6

Q1 Q8

4
Q4 – efficient algorithm
s[] : s[i] = sigma(x[j]), j <= i
x[], k : input
hash[] : infinity.
hash[0] = 0
for(i = 1; i <= n; i++)
hash[s[i]] = min(hash[s[i]], i);
for(i = 1; i <= n; i++)
index = hash[s[i]-k]
if(index <= i)
return true // pair(index+1, i)
return false
2013/11/6

Q1 Q8

5
Q5
• Given a 2-dimensional n x n array (or matrix)
of 0/1 integers, design a linear time algorithm
to find a sub-square (a sub-square matrix with
consecutive indices in both dimensions) with
the largest size such that all the elements in the
sub-square are equal to 1.

2013/11/6

Q1 Q8

6
Q5 – efficient algorithm
m[][], n : input
dp[][] : init zero
r = 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(m[i][j] == 1)
dp[i][j]=min(dp[i-1][j],
dp[i][j-1],dp[i-1][j-1])+1;
r = max(r, dp[i][j])
return r;

2013/11/6

Q1 Q8

7
Q6
• Given an undirected graph G, a vertex subset I is
called an independent set of G if for every pair of
vertices u and v in I, uv not in E(G). The independent
set problem is to find an independent set with
maximum size. Design a linear time algorithm to
solve the problem on undirected trees. Can you
extend your algorithm to solve the weighted case?
(That is, each node is associated with a positive
weight, and you are asked to find an independent set
with maximum weight sum.)
2013/11/6

Q1 Q8

8
Q6 – efficient algorithm
weight[] : input
dp[n][2] : init zero.
dfs(1, -1)
dfs(nd, p) {
for(neighbor e for nd) {
if(e == p)
continue
dfs(e, nd)
dp[nd][0]+=max(dp[e][0], dp[e][1])
dp[nd][1]+=dp[e][0];
}
dp[nd][1] += weight[nd];
}
return max(dp[1][0], dp[1][1])
2013/11/6

Q1 Q8

9
Q7
• Let x1, x2, … , xn be a sequence of real numbers (not
necessarily positive). Design an algorithm to find a
substring xi , xi+1, …, xj such that the product of the
numbers in it is maximum over all substrings. The
product of the empty substring is defined as 1.

2013/11/6

Q1 Q8

10
Q7 – efficient algorithm
x[], n : input
mndp[] :
mxdp[] :
mndp[0] = +oo
mxdp[0] = -oo
r = 1
for(i = 1; i <= n; i++) {
mxdp[i] = max(x[i], mxdp[i-1]*x[i],
mndp[i-1]*x[i])
mndp[i] = min(x[i], mxdp[i-1]*x[i],
mndp[i-1]*x[i])
r = max(r, mxdp[i], mndp[i])
}
return r
2013/11/6

Q1 Q8

11
Q8
• Given a sequence of real numbers x1, x2, …, xn
(not necessarily positive), n > 1, and an
integer L, 1 <= L <= n. Design an algorithm
to find a substring xi, xi+1, …, xj such that its
length j - i+1 <= L and the sum of the
numbers in it is maximum over all substrings
with length not greater than L. Your algorithm
should run in O(n) time (note: not O(nL)).
2013/11/6

Q1 Q8

12
Q8 – efficient algorithm
x[], n, L : input
Q[] : monotone queue
s[] : s[0] = 0
head = tail = 0
r = -oo
for(i = 1; i <= n; i++)
s[i] = s[i-1] + x[i]
while(head < tail && s[Q[head]] >= s[i])
tail--;
Q[tail++] = s[i];
r = max(r, s[i], s[i]-s[Q[head]]);
while(head < tail && Q[head] <= i-L)
head++;
return r;
2013/11/6

Q1 Q8

13
Q9
• Given a sequence of objects where each object
is associated with a value and a weight, design
an algorithm to find a subsequence such that
its corresponding value sequence is increasing
and its weights’ sum is maximized.

2013/11/6

Q1 Q8

14
Q9 – efficient algorithm
x[], w[], n: input
discretization for x[] in [1, n] --- O(nlgn)
dynamic_RMQ_struct S : init zero.
r = w[1]
for(i = 1; i <= n; i++)
v = S.query_max(0, x[i]-1) --- O(lg n)
S.updateVal(x[i], v+w[i]) --- O(lg n)
r = max(r, v+w[i])
return r

2013/11/6

Q1 Q8

15
Q10
• Design an algoritm to solve Problem 10154
“Weights and Measures” in the web site:
http://acm.uva.es/problemset/.

2013/11/6

Q1 Q8

16
Q10 – efficient algorithm(1)
A[].first – strong, A[].second – weight
sort(A, A+n)
dp[0] = 0
for(i = 1; i <= n; i++)
dp[i] = +oo;
for(i = 0; i < n; i++)
for(j = n; j >= 1; j--)
if(dp[j-1]+A[i].w <= A[i].s)
dp[j] = min(dp[j], dp[j-1]+A[i].w)
for(i = n; ; i--)
if(dp[i] != +oo)
return i;
2013/11/6

Q1 Q8

17
Q10 – efficient algorithm(2)
A[].first – weight, A[].second – strong
sort(A, A+n)
using pattern as inserting sort.
for(i = 1; i <= n; i++) {
..xx..
}

2013/11/6

Q1 Q8

18

More Related Content

PDF
Prim algorithm
University of Potsdam
 
PDF
Scribed lec8
Praveen Kumar
 
PDF
Iterative Compression
ASPAK2014
 
PDF
Paths and Polynomials
ASPAK2014
 
PDF
Important Cuts and (p,q)-clustering
ASPAK2014
 
PDF
Programming workshop
Sandeep Joshi
 
PPTX
Datastructure tree
rantd
 
Prim algorithm
University of Potsdam
 
Scribed lec8
Praveen Kumar
 
Iterative Compression
ASPAK2014
 
Paths and Polynomials
ASPAK2014
 
Important Cuts and (p,q)-clustering
ASPAK2014
 
Programming workshop
Sandeep Joshi
 
Datastructure tree
rantd
 

What's hot (20)

PDF
Minimum spanning tree
Amit Kumar Rathi
 
PDF
IRJET- Global Accurate Domination in Jump Graph
IRJET Journal
 
PDF
13 - 06 Feb - Dynamic Programming
Neeldhara Misra
 
PDF
Bidimensionality
ASPAK2014
 
PPT
Algorithm
sultanarun
 
PPT
5.3 dynamic programming
Krish_ver2
 
PDF
Treewidth and Applications
ASPAK2014
 
PDF
cryptography_non_abeliean
NARAYANASWAMY CHANDRAMOWLISWARAN
 
PPTX
Minimal spanning tree class 15
Kumar
 
PPTX
Dynamic Programming - Matrix Chain Multiplication
Pecha Inc.
 
PDF
The Exponential Time Hypothesis
ASPAK2014
 
PDF
Brief summary of signals
aroosa khan
 
PPT
Dynamic1
MyAlome
 
PPT
Muchtadi
Niranjan Patidar
 
PPT
5.1 greedy
Krish_ver2
 
PDF
Dynamic Parameterized Problems - Algorithms and Complexity
cseiitgn
 
PPT
Maximum clique detection algorithm
Abhishek Kona
 
PDF
MinFill_Presentation
Anna Lasota
 
PPTX
Daa unit 4
Abhimanyu Mishra
 
PPT
5.3 dynamic programming 03
Krish_ver2
 
Minimum spanning tree
Amit Kumar Rathi
 
IRJET- Global Accurate Domination in Jump Graph
IRJET Journal
 
13 - 06 Feb - Dynamic Programming
Neeldhara Misra
 
Bidimensionality
ASPAK2014
 
Algorithm
sultanarun
 
5.3 dynamic programming
Krish_ver2
 
Treewidth and Applications
ASPAK2014
 
cryptography_non_abeliean
NARAYANASWAMY CHANDRAMOWLISWARAN
 
Minimal spanning tree class 15
Kumar
 
Dynamic Programming - Matrix Chain Multiplication
Pecha Inc.
 
The Exponential Time Hypothesis
ASPAK2014
 
Brief summary of signals
aroosa khan
 
Dynamic1
MyAlome
 
5.1 greedy
Krish_ver2
 
Dynamic Parameterized Problems - Algorithms and Complexity
cseiitgn
 
Maximum clique detection algorithm
Abhishek Kona
 
MinFill_Presentation
Anna Lasota
 
Daa unit 4
Abhimanyu Mishra
 
5.3 dynamic programming 03
Krish_ver2
 
Ad

Similar to Aaex2 group2 (20)

PPTX
Aaex4 group2(中英夾雜)
Shiang-Yun Yang
 
PPTX
Aaex3 group2
Shiang-Yun Yang
 
PDF
Dynamic programming
PrudhviVuda
 
PPTX
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
shesnasuneer
 
PPTX
LectureSlidData_sturcture_algorithm_v2.pptx
abhaysharma999437
 
DOCX
Arrays
poonamchopra7975
 
PPT
Time complexity.ppt
YekoyeTigabuYeko
 
PPT
Time complexity.pptr56435 erfgegr t 45t 35
DickyNsjg1
 
PPT
how to calclute time complexity of algortihm
Sajid Marwat
 
DOCX
C programs
Lakshmi Sarvani Videla
 
PPT
How to calculate complexity in Data Structure
debasisdas225831
 
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
PPT
Lec03 04-time complexity
Abbas Ali
 
DOCX
Standard_DSA_Algorithms.docx edited.docx
rahuljanawad17
 
PDF
Insertion sort
Abdelrahman Saleh
 
PPTX
Computational Complexity.pptx
EnosSalar
 
PPT
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
DOC
algorithm Unit 2
Monika Choudhery
 
DOC
Unit 2 in daa
Nv Thejaswini
 
PPT
Lowest common ancestor
Shakil Ahmed
 
Aaex4 group2(中英夾雜)
Shiang-Yun Yang
 
Aaex3 group2
Shiang-Yun Yang
 
Dynamic programming
PrudhviVuda
 
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
shesnasuneer
 
LectureSlidData_sturcture_algorithm_v2.pptx
abhaysharma999437
 
Time complexity.ppt
YekoyeTigabuYeko
 
Time complexity.pptr56435 erfgegr t 45t 35
DickyNsjg1
 
how to calclute time complexity of algortihm
Sajid Marwat
 
How to calculate complexity in Data Structure
debasisdas225831
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
Lec03 04-time complexity
Abbas Ali
 
Standard_DSA_Algorithms.docx edited.docx
rahuljanawad17
 
Insertion sort
Abdelrahman Saleh
 
Computational Complexity.pptx
EnosSalar
 
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
algorithm Unit 2
Monika Choudhery
 
Unit 2 in daa
Nv Thejaswini
 
Lowest common ancestor
Shakil Ahmed
 
Ad

More from Shiang-Yun Yang (15)

PPTX
User interface
Shiang-Yun Yang
 
PPTX
Polarity analysis for sentiment classification
Shiang-Yun Yang
 
PPTX
文明的進程第十組
Shiang-Yun Yang
 
PPTX
計算幾何論文報告 Minimum local disk cover sets
Shiang-Yun Yang
 
PPTX
N grams as linguistic features
Shiang-Yun Yang
 
PDF
軍事報告 電磁砲
Shiang-Yun Yang
 
PDF
第二十組福斯汽車
Shiang-Yun Yang
 
PPTX
敏捷簡報
Shiang-Yun Yang
 
PDF
計算型智慧論文報告 Building optimal regression tree ...
Shiang-Yun Yang
 
PPTX
Aaex7 group2(中英夾雜)
Shiang-Yun Yang
 
PPTX
Rpg 角色扮演遊戲 – 初探
Shiang-Yun Yang
 
PPTX
Aaex6 group2(中英夾雜)
Shiang-Yun Yang
 
PPTX
Aaex5 group2(中英夾雜)
Shiang-Yun Yang
 
PPTX
通識報告
Shiang-Yun Yang
 
PDF
Alex1 group2
Shiang-Yun Yang
 
User interface
Shiang-Yun Yang
 
Polarity analysis for sentiment classification
Shiang-Yun Yang
 
文明的進程第十組
Shiang-Yun Yang
 
計算幾何論文報告 Minimum local disk cover sets
Shiang-Yun Yang
 
N grams as linguistic features
Shiang-Yun Yang
 
軍事報告 電磁砲
Shiang-Yun Yang
 
第二十組福斯汽車
Shiang-Yun Yang
 
敏捷簡報
Shiang-Yun Yang
 
計算型智慧論文報告 Building optimal regression tree ...
Shiang-Yun Yang
 
Aaex7 group2(中英夾雜)
Shiang-Yun Yang
 
Rpg 角色扮演遊戲 – 初探
Shiang-Yun Yang
 
Aaex6 group2(中英夾雜)
Shiang-Yun Yang
 
Aaex5 group2(中英夾雜)
Shiang-Yun Yang
 
通識報告
Shiang-Yun Yang
 
Alex1 group2
Shiang-Yun Yang
 

Recently uploaded (20)

PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Software Development Company | KodekX
KodekX
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
This slide provides an overview Technology
mineshkharadi333
 
Doc9.....................................
SofiaCollazos
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 

Aaex2 group2

  • 1. Advanced Algorithms Exercise 2 Group 2 陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧 2013/11/6 Q1 Q8 1
  • 2. Q3 • Given an integer (not necessary a decimal number) with n digits, we want to remove m (m < n) digits from this number such that the resulting number is as large as possible. Design an O(n) time algorithm to solve it. • Ex. n = 9, m = 3 input : 987645821 remove bold place, maximum result = 987821 2013/11/6 Q1 Q8 2
  • 3. Q3 – efficient algorithm s[] : digit as [0, n-1] r[] : maximum result (stack) for(i = top = 0; i < n; i++) if(top + n – i <= n) while(i < n) r[top++] = s[i++] break; while(top > 0 && s[i] > r[top-1]) top--; if(top + n – i <= n) break; r[top++] = s[i] return r[]; 2013/11/6 Q1 Q8 3
  • 4. Q4 • Let x1, x2, … , xn be a sequence of integers and k be another given integer. Design an algorithm to find a subsequence xi , xi+1, …, xj (of consecutive elements) such that the sum of the numbers in it is exactly k if there does exist such a subsequence. 2013/11/6 Q1 Q8 4
  • 5. Q4 – efficient algorithm s[] : s[i] = sigma(x[j]), j <= i x[], k : input hash[] : infinity. hash[0] = 0 for(i = 1; i <= n; i++) hash[s[i]] = min(hash[s[i]], i); for(i = 1; i <= n; i++) index = hash[s[i]-k] if(index <= i) return true // pair(index+1, i) return false 2013/11/6 Q1 Q8 5
  • 6. Q5 • Given a 2-dimensional n x n array (or matrix) of 0/1 integers, design a linear time algorithm to find a sub-square (a sub-square matrix with consecutive indices in both dimensions) with the largest size such that all the elements in the sub-square are equal to 1. 2013/11/6 Q1 Q8 6
  • 7. Q5 – efficient algorithm m[][], n : input dp[][] : init zero r = 0; for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) if(m[i][j] == 1) dp[i][j]=min(dp[i-1][j], dp[i][j-1],dp[i-1][j-1])+1; r = max(r, dp[i][j]) return r; 2013/11/6 Q1 Q8 7
  • 8. Q6 • Given an undirected graph G, a vertex subset I is called an independent set of G if for every pair of vertices u and v in I, uv not in E(G). The independent set problem is to find an independent set with maximum size. Design a linear time algorithm to solve the problem on undirected trees. Can you extend your algorithm to solve the weighted case? (That is, each node is associated with a positive weight, and you are asked to find an independent set with maximum weight sum.) 2013/11/6 Q1 Q8 8
  • 9. Q6 – efficient algorithm weight[] : input dp[n][2] : init zero. dfs(1, -1) dfs(nd, p) { for(neighbor e for nd) { if(e == p) continue dfs(e, nd) dp[nd][0]+=max(dp[e][0], dp[e][1]) dp[nd][1]+=dp[e][0]; } dp[nd][1] += weight[nd]; } return max(dp[1][0], dp[1][1]) 2013/11/6 Q1 Q8 9
  • 10. Q7 • Let x1, x2, … , xn be a sequence of real numbers (not necessarily positive). Design an algorithm to find a substring xi , xi+1, …, xj such that the product of the numbers in it is maximum over all substrings. The product of the empty substring is defined as 1. 2013/11/6 Q1 Q8 10
  • 11. Q7 – efficient algorithm x[], n : input mndp[] : mxdp[] : mndp[0] = +oo mxdp[0] = -oo r = 1 for(i = 1; i <= n; i++) { mxdp[i] = max(x[i], mxdp[i-1]*x[i], mndp[i-1]*x[i]) mndp[i] = min(x[i], mxdp[i-1]*x[i], mndp[i-1]*x[i]) r = max(r, mxdp[i], mndp[i]) } return r 2013/11/6 Q1 Q8 11
  • 12. Q8 • Given a sequence of real numbers x1, x2, …, xn (not necessarily positive), n > 1, and an integer L, 1 <= L <= n. Design an algorithm to find a substring xi, xi+1, …, xj such that its length j - i+1 <= L and the sum of the numbers in it is maximum over all substrings with length not greater than L. Your algorithm should run in O(n) time (note: not O(nL)). 2013/11/6 Q1 Q8 12
  • 13. Q8 – efficient algorithm x[], n, L : input Q[] : monotone queue s[] : s[0] = 0 head = tail = 0 r = -oo for(i = 1; i <= n; i++) s[i] = s[i-1] + x[i] while(head < tail && s[Q[head]] >= s[i]) tail--; Q[tail++] = s[i]; r = max(r, s[i], s[i]-s[Q[head]]); while(head < tail && Q[head] <= i-L) head++; return r; 2013/11/6 Q1 Q8 13
  • 14. Q9 • Given a sequence of objects where each object is associated with a value and a weight, design an algorithm to find a subsequence such that its corresponding value sequence is increasing and its weights’ sum is maximized. 2013/11/6 Q1 Q8 14
  • 15. Q9 – efficient algorithm x[], w[], n: input discretization for x[] in [1, n] --- O(nlgn) dynamic_RMQ_struct S : init zero. r = w[1] for(i = 1; i <= n; i++) v = S.query_max(0, x[i]-1) --- O(lg n) S.updateVal(x[i], v+w[i]) --- O(lg n) r = max(r, v+w[i]) return r 2013/11/6 Q1 Q8 15
  • 16. Q10 • Design an algoritm to solve Problem 10154 “Weights and Measures” in the web site: http://acm.uva.es/problemset/. 2013/11/6 Q1 Q8 16
  • 17. Q10 – efficient algorithm(1) A[].first – strong, A[].second – weight sort(A, A+n) dp[0] = 0 for(i = 1; i <= n; i++) dp[i] = +oo; for(i = 0; i < n; i++) for(j = n; j >= 1; j--) if(dp[j-1]+A[i].w <= A[i].s) dp[j] = min(dp[j], dp[j-1]+A[i].w) for(i = n; ; i--) if(dp[i] != +oo) return i; 2013/11/6 Q1 Q8 17
  • 18. Q10 – efficient algorithm(2) A[].first – weight, A[].second – strong sort(A, A+n) using pattern as inserting sort. for(i = 1; i <= n; i++) { ..xx.. } 2013/11/6 Q1 Q8 18