Presentation is loading. Please wait.

Presentation is loading. Please wait.

IMPROVING PSEUDOCODE CS16: Introduction to Data Structures & Algorithms Thursday, February 26, 2015 1.

Similar presentations


Presentation on theme: "IMPROVING PSEUDOCODE CS16: Introduction to Data Structures & Algorithms Thursday, February 26, 2015 1."— Presentation transcript:

1 IMPROVING PSEUDOCODE CS16: Introduction to Data Structures & Algorithms Thursday, February 26, 2015 1

2 Clean Your Code Errors per line ~ constant Fewer lines  fewer errors overall Fewer lines are easier to grade More likely to receive credit Cleaner code reflects cleaner thinking and a better understanding of the material Let’s work through an example… Thursday, February 26, 2015 2

3 Lowest Common Ancestor (LCA) Problem: Given two nodes u and v, determine the deepest node that’s an ancestor of both. Thursday, February 26, 2015 3 A B D C GH E F IJ K uvLCA(u, v) CDA JEB GJA GCC

4 Thursday, February 26, 2015 4 function LCA(u, v): lca = null udepth = T.depth(u) vdepth = T.depth(v) if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if udepth > vdepth then u = T.parent(u) udepth = udepth – 1 else if vdepth > udepth v = T.parent(v) vdepth = vdepth – 1 else u = T.parent(u); udepth = udepth - 1 v = T.parent(v); vdepth = vdepth – 1 return lca

5 Thursday, February 26, 2015 5 function LCA(u, v): lca = null udepth = T.depth(u) vdepth = T.depth(v) if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if udepth > vdepth then u = T.parent(u) udepth = udepth – 1 else if vdepth > udepth v = T.parent(v) vdepth = vdepth – 1 else u = T.parent(u); udepth = udepth - 1 v = T.parent(v); vdepth = vdepth – 1 return lca What are the inputs? The output?

6 Thursday, February 26, 2015 6 function LCA(u, v): // Input: two nodes u, v // Output: the lowest common ancestor of u and v lca = null udepth = T.depth(u) vdepth = T.depth(v) if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if udepth > vdepth then u = T.parent(u) udepth = udepth – 1 else if vdepth > udepth v = T.parent(v) vdepth = vdepth – 1 else u = T.parent(u); udepth = udepth - 1 v = T.parent(v); vdepth = vdepth – 1 return lca

7 Thursday, February 26, 2015 7 function LCA(u, v): // Input: two nodes u, v // Output: the lowest common ancestor of u and v lca = null udepth = T.depth(u) vdepth = T.depth(v) if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if udepth > vdepth then u = T.parent(u) udepth = udepth – 1 else if vdepth > udepth v = T.parent(v) vdepth = vdepth – 1 else u = T.parent(u); udepth = udepth - 1 v = T.parent(v); vdepth = vdepth – 1 return lca Where does T come from?

8 Thursday, February 26, 2015 8 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null udepth = T.depth(u) vdepth = T.depth(v) if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if udepth > vdepth then u = T.parent(u) udepth = udepth – 1 else if vdepth > udepth v = T.parent(v) vdepth = vdepth – 1 else u = T.parent(u); udepth = udepth - 1 v = T.parent(v); vdepth = vdepth – 1 return lca

9 Ways to improve psuedocode Clarify your input and output with comments This is a good habit to get into and makes methods easily understandable for somebody who has never seen them before Make sure all necessary arguments are included as parameters Thursday, February 26, 2015 9

10 10 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null udepth = T.depth(u) vdepth = T.depth(v) if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if udepth > vdepth then u = T.parent(u) udepth = udepth – 1 else if vdepth > udepth v = T.parent(v) vdepth = vdepth – 1 else u = T.parent(u); udepth = udepth - 1 v = T.parent(v); vdepth = vdepth – 1 return lca Needlessly complex

11 Thursday, February 26, 2015 11 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null udepth = T.depth(u) vdepth = T.depth(v) if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca Now irrelevant

12 Ways to improve psuedocode Get rid of unnecessary variables Using variables for information which can be accurately found elsewhere leaves room for careless errors In this example there is no need for udepth and vdepth when the tree keeps track of the node’s depth Thursday, February 26, 2015 12

13 Thursday, February 26, 2015 13 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca

14 Thursday, February 26, 2015 14 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if (T.isroot(u) == true) or (T.isroot(v) == true) then lca = T.root while (lca == null) do if (u == v) then lca = u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca Redundant equality checks

15 Thursday, February 26, 2015 15 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if T.isroot(u) or T.isroot(v) then lca = T.root while (lca == null) do if (u == v) then lca = u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca

16 Thursday, February 26, 2015 16 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if T.isroot(u) or T.isroot(v) then lca = T.root while (lca == null) do if (u == v) then lca = u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca

17 Thursday, February 26, 2015 17 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if T.isroot(u) or T.isroot(v) then lca = T.root while (lca == null) do if (u == v) then lca = u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca Just removing some whitespace to fit on slides

18 Ways to improve psuedocode If a method returns a boolean, there is no need to check if return value == true Logical operators can be used on boolean return values !T.isroot(u) equals T.isroot(u)==false Thursday, February 26, 2015 18

19 Thursday, February 26, 2015 19 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if T.isroot(u) or T.isroot(v) then lca = T.root while (lca == null) do if (u == v) then lca = u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca It’s the answer, return it!

20 Thursday, February 26, 2015 20 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if T.isroot(u) or T.isroot(v) then lca = T.root return lca while (lca == null) do if (u == v) then lca = u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca It’s the answer, return it!

21 Thursday, February 26, 2015 21 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if T.isroot(u) or T.isroot(v) then lca = T.root return lca while (lca == null) do if (u == v) then lca = u return lca else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca

22 Ways to improve psuedocode As soon as you know you’ve found the answer, return it! This avoids going through unnecessary code Thursday, February 26, 2015 22

23 Thursday, February 26, 2015 23 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if T.isroot(u) or T.isroot(v) then lca = T.root return lca while (lca == null) do if (u == v) then lca = u return lca else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) return lca Condition is irrelevant

24 Thursday, February 26, 2015 24 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v lca = null if T.isroot(u) or T.isroot(v) then lca = T.root return lca repeat if (u == v) then lca = u return lca else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v) lca is no longer used

25 Thursday, February 26, 2015 25 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v if T.isroot(u) or T.isroot(v) then return T.root repeat if (u == v) then return u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v)

26 Thursday, February 26, 2015 26 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v if T.isroot(u) or T.isroot(v) then return T.root repeat if (u == v) then return u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v)

27 Ways to improve psuedocode If a variable is only used for the purpose of returning something, simply return the value This avoids having to keep track of an unnecessary variable and makes your code shorter and clearer Thursday, February 26, 2015 27

28 Thursday, February 26, 2015 28 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v if T.isroot(u) or T.isroot(v) then return T.root repeat if (u == v) then return u else if T.depth(u) > T.depth(v) then u = T.parent(u) else if T.depth(v) > T.depth(u) v = T.parent(v) else u = T.parent(u) v = T.parent(v)

29 Thursday, February 26, 2015 29 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v while T.depth(u) > T.depth(v) u = T.parent(u) while T.depth(v) > T.depth(u) v = T.parent(v) if T.isroot(u) or T.isroot(v) then return T.root repeat if (u == v) then return u else u = T.parent(u) v = T.parent(v)

30 Ways to improve psuedocode If you know you will not enter a conditional after one iteration through a while loop, attempt to use two separate while loops to simplify In this example, if u is at a lower depth than v, the algorithm will not allow u to get to a higher depth than v. Therefore, it is unnecessary to continue checking both of these within one while loop Thursday, February 26, 2015 30

31 Thursday, February 26, 2015 31 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v while T.depth(u) > T.depth(v) u = T.parent(u) while T.depth(v) > T.depth(u) v = T.parent(v) if T.isroot(u) or T.isroot(v) then return T.root repeat if (u == v) then return u else u = T.parent(u) v = T.parent(v)

32 Thursday, February 26, 2015 32 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v while T.depth(u) > T.depth(v) u = T.parent(u) while T.depth(v) > T.depth(u) v = T.parent(v) if T.isroot(u) or T.isroot(v) or u == v then return u repeat if (u == v) then return u else u = T.parent(u) v = T.parent(v)

33 Thursday, February 26, 2015 33 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v while T.depth(u) > T.depth(v) u = T.parent(u) while T.depth(v) > T.depth(u) v = T.parent(v) if T.isroot(u) or T.isroot(v) or u == v then return u repeat if (u == v) then return u else u = T.parent(u) v = T.parent(v) Can be simplified!

34 Ways to improve psuedocode Avoid unnecessary conditional checks In this example, after the two while loops, u and v have the same depth. If either is a root, they both are the same root Try to be as concise as possible! Thursday, February 26, 2015 34

35 Thursday, February 26, 2015 35 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v while T.depth(u) > T.depth(v) u = T.parent(u) while T.depth(v) > T.depth(u) v = T.parent(v) if u == v then return u repeat if (u == v) then return u else u = T.parent(u) v = T.parent(v) Condense into a single loop!

36 Thursday, February 26, 2015 36 function LCA(u, v, T): // Input: two nodes u, v in a tree T // Output: the lowest common ancestor of u and v while T.depth(u) > T.depth(v) u = T.parent(u) while T.depth(v) > T.depth(u) v = T.parent(v) while u != v then u = T.parent(u) v = T.parent(v) return u From a clunky 19 lines to an elegant 8 line beauty!

37 Improve psuedocode – yes you can! Now that you have seen how easily psuedocode can be simplified, you are expected to be making similar improvements to your own psuedocode Good psuedocode is BOTH accurate and concise Thursday, February 26, 2015 37


Download ppt "IMPROVING PSEUDOCODE CS16: Introduction to Data Structures & Algorithms Thursday, February 26, 2015 1."

Similar presentations


Ads by Google