hwk-challenge - 2 You are given a tree T , having n...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
TA: Catalin-Stefan Tiseanu Challenge Problem Set Clyde Kruskal Here are a couple of challenge homework problems, not to be turned in. You can discuss the answers with Catalin-Stefan Tiseanu during oFce hours. Problem 1 An inversion in an array is de±ned as a pair of indices ( i, j ) with i < j and A [ i ] > A [ j ] . ²or example, the array [ 3, 2, 4, 1 ] has 4 inversions (1 with all three other elements and 3 with 2). Give an eFcient algorithm to compute the number of inversions in an array A of n integers. Hint : Your algorithm should take O ( n lg n ) time. Hint : Think about adapting a sorting algorithm you studied in class. Problem
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2 You are given a tree T , having n vertices (rooted at vertex r for convenience). You are also given k pairs ( x i , y i ) , 1 ≤ i ≤ k . The task is to output for each pair ( x i , y i ) whether x i is an ancestor of y i in tree T (that is, output yes if x i is an ancestor of y i , and no if it is not). Note that x i is an ancestor of y i can be reframed as y i is in the subtree rooted at x i for convenience. Your algorithm should take O ( n + k ) time. Hint : Think about the graph traversal methods you learned in class. 1...
View Full Document

This note was uploaded on 01/13/2012 for the course CMSC 351 taught by Professor Staff during the Fall '11 term at University of Louisville.

Ask a homework question - tutors are online