简体   繁体   中英

List of Comparators in Sorting Networks

I have a question in my homework document, and I'm having hard to time to visualize and understand the question. Question is the following:

We can represent an n-input comparison network with c comparators as a list of c pairs of integers in the range from 1 to n. If two pairs contain an integer in common, the order of the corresponding comparators in the network is determined by the order of the pairs in the list. Given this representation, describe an O(n + c)-time (serial) algorithm for determining the depth of a comparison network.

What does it mean to have pairs of integers in the context of comparison networks? Normally we used the notation below for denoting a comparison network where each horizontal line represents a number.

在此处输入图片说明

It means that if you have a pair (1, 2), that's one of those vertical lines, namely the one that connects horizontal lines 1 and 2.

So the top left part of this picture would be represented as (1, 2) (3, 4) (1, 3) (2, 4).

左上方

The depth of just that part is 2.

for i = 1, n 
  depth[i] = 0

total_depth = 0
for j = 1, c
  i1 = comparators[j].entry1
  i2 = comparators[j].entry2
  new_depth = 1 + max(depth[i1], depth[i2])
  depth[i1] = new_depth
  depth[i2] = new_depth
  total_depth = max(total_depth, new_depth)

print(total_depth)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM