1

I am trying out this Google Codejam problem which says to find out number of hamiltonian paths if we remove k edges from a complete graph

link to Question

https://code.google.com/codejam/contest/32004/dashboard#s=p2

I figured out we can use inclusion exclusion principle to find out the number

but my problem is how to determine the number of path when we are considering that some 'x' number of edges have been removed from the complete graph(the edges removed are given)

Ezio
  • 723
  • 6
  • 14

1 Answers1

0

The idea is to count permutations instead of counting paths. This way, each path would be taken into account 2*n times.

The total number if permutations is n!.

Let's use the inculsion-exlusion principle to count bad cycles. If one edge is banned, there are 2*n * (n-2)! paths that contain this edge (we place two adjacent vertices together and the rest goes anywhere).

If there are several banned edges, all vetrices are divided into several independent groups (they form chains connected by these edges). There are two ways to place each group (as it can be reversed). All groups can be arbitrarily permuted with each other. The rest of the elements can be placed anywhere (it would contribute as a binomial coefficient times some factorial). There is one more caveat: a chain can wrap around. But there can be at most one such chain. So we can iterate over the chain that wraps and count the number of ways to place the rest using the algorithm described above.

kraskevich
  • 18,368
  • 4
  • 33
  • 45
  • If one edge is banned then we are considering the two vertices connected as a single vertice ? and can you please explain what is the chain wrap around ? – Ezio Jan 17 '17 at 09:46
  • @Ezio Yes, two connected vetrices is treated as a single vertex. Wrapping around means that, say an edge (1,2) is banned, 1 is the first element of the permutation and 2 is the last one. – kraskevich Jan 17 '17 at 09:48
  • So suppose there are two edges removed from a complete graph of n vertices , then how can we mathematically represent the inclusion exclusion formula ? – Ezio Jan 17 '17 at 09:51
  • @Ezio we need to count the number of permutations with exactly one bad edge and subtract the number of permutations with 2 bad edges from it. – kraskevich Jan 17 '17 at 10:32
  • Actually my problem is how to count the number of permutation with some x bad edges i can apply the inclusion exclusion principle but i am having problem on how to calculate the number of permutation when we consider lets say x bad edges – Ezio Jan 17 '17 at 10:42