GATE / CS ARCHIVE

GATE CSE 2021 SET 2 / QUESTION 55

GATE CSE 2021 Set 2 Q55

Algorithms · Graph Algorithms · Full GATE CSE 2021 Set 2 paper

GATE 2021 Set 2, Q55 2 marks · NAT
Report issue

Graph Algorithms

Question 55, GATE 2021: DAG vertices s,a,b,c,d,e,f,g,t. Edges with weights: s→a 9,s→c 1,a→b 1,a→d 1,c→d 1,c→f 9,d→e 9,d→g 9,b→e 1,f→g 1,g→t 1,e→t 9. Path score is edge-weight product; vertex score is largest path score from s; score(s)=1. Find sum of all vertex scores.
Tap to enlarge
Answer & solution

Official answer: 929

Step through it (10 steps)

Process vertices topologically with Q(v)=max over predecessors u of Q(u)*weight(u, v), starting Q(s)=1. The diagram yields Q(a)=9, Q(c)=1, Q(b)=9, Q(d)=max(1, 9)=9, Q(f)=9, Q(e)=max(9, 81)=81, Q(g)=max(9, 81)=81, Q(t)=max(81, 729)=729. Sum=1+9+1+9+9+9+81+81+729=929.

A topological dynamic program initializes score(s)=1 and all other scores=0, then for each edge u→v sets score(v)=max(score(v),score(u)×weight). A predecessor ordering such as s,a,b,c,d,e,f,g,t suffices. It runs in O(V+E), since a DAG has no profitable cycle to revisit. Store parent pointers when maximizing if an actual maximizing path is also wanted.

Original paper, page 88 · Answer key