algorithm - Merging sorted lists without comparison key -


say have following lists:

list 1: x, y, z list 2: w, x list 3: u 

and want merge them such order among each individual list respected. solution above problem might w, x, y, z, u.

this problem easy if have comparison key (e.g. string comparison; < z), gives reference element's position relative other elements in combined list. case when don't have key? above problem, restate problem follows:

x < y , y < z , w < x x, y, z, w, u in {0, 1, 2, 3, 4} 

the way i'm solving type of problem model problem constraint satisfaction problem -- run ac3 arc consistency algorithm eliminate inconsistent values, , run recursive backtracking algorithm make assignments. works fine, seems overkill.

is there general algorithm or simpler approach confront type of problem?

  1. construct graph node every letter in lists.

    x    y    z   w    u 
  2. add directed edge letter x letter y every pair of consecutive letters in list.

    x -> y -> z ^ | w    u 
  3. topologically sort graph nodes obtain final list satisfies constraints.

    if there ever cycle in graph, topological sorting algorithm detect cycle, revealing contradiction in constraints induced original lists.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -