Algorithm to find a group seating arrangement for an open book test -
you planning group seating arrangement open book test given list of students, v different schools participate. assuming fact students known each other directly or indirectly cheat more compared unknown people sitting together. suppose given lookup table t t[u] u ? v list of students u knows. if u knows v, v knows u. required arrange seating such student @ table doesn't knows other student sitting @ same table either directly or through other student sitting @ same table. example, if x knows y, , y knows z, x, y, z can sit @ same table. describe efficient algorithm that, given v , t, returns minimum number of tables needed achieve requirement. analyze running time of algorithm.
follow student relations out 2 edges, graph:
a - e - j \ q b - d \ t r - w - x - y - z
all students in same subgraph have separated, minimum number of tables 1 each students in largest group - in example largest subgraph r-w-x-y-z
, 5 tables.
untested python pseudocode:
# given student list # b c d e f j q r t w x y z # start chain @ # b c d e f j q r t w x y z # . # visit friends of # b c d e f j q r t w x y z # . . # visit friends of a's friends # b c d e f j q r t w x y z # . . . . # if e , j friends, don't double-count # count of 4 starting @ person # repeat students # report longest chain. friendcounts = {} def countfriendsof(t, student, friendtracker, moresteps=2): friendtracker[student] = true #quicker set regardless, #than check if it's set if not moresteps: return friend in t[student]: countfriendsof(t, friend, friendtracker, moresteps - 1) return friendtracker u in v: friends = countfriendsof(t, u, friendtracker={}) friendcounts[u] = (len(friends), friends) results = sorted(friendcounts.items(), key=lambda x: x[1][0], reverse=true) (student, (friendcount, friends)) = results[0] print "the smallest number of tables is:", friendcount print "mandated friend group of:", student print pprint import pprint pprint(friends)
analyze running time of algorithm.
analysis: fine on computer more powerful snowglobe.
not sure. best case: students have no friends - linear respect number of students. o(n). worst case: every student friends every other student, lookups every student every student, o(n^3). ew.
it running more o(n^2) until realised version wrong.
this version not-definitely-wrong, isn't definitely-right.
i didn't start recursive solution, ended going way. friendtracker use nasty side-effect, , recursive call not tail recursion optimizable. not python that,
Comments
Post a Comment