How to iterate through a tree in spiral order? - data-structures

1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
the output in spiral order should be
1 3 2 4 5 6 7 15 14 13 12 11 10 9 8

Think about how you would iterate through the root node and the first level.
Can you write a function for that behavior, and call it recursively?

This is really a variation of a breadth-first search. A breadth-first search uses a queue to get a list of the nodes at the next level down. A queue is a FIFO (first in first out). If you reverse the order at each level you'll get this effect so you need a LIFO (last in first out) instead, otherwise known as a stack.

Related

Travel randomly through a matrix

I have just finished a project where I implemented the recursive backtracking algorithm for a maze generator. Since then I have been looking for an algorithm that goes through a matrix but passing through each square only once. Just like in the recursive backtracking algorithm but without "going back". Is there any algorithm for this?
This is an example of what I mean with a 5*5 matrix:
5 4 1 20 21
6 3 2 19 22
7 8 17 18 23
10 9 16 15 24
11 12 13 14 25
As you can see you can start from square 1 and end on 25 without repeating any other square.
Thanks for your time.

Build a specific leftist tree?

I was practicing leftist trees and saw an example of min height-biased leftist tree on the textbook:
2
/ \
7 50
/ /
11 80
/
13
The question is, can I use only insertions to build this example?
I tried the following insertion sequence:
2 7 11 13 50 80
and it turns out to be this one:
2
/ \
11 7
/ \ /
13 50 80
So how can I achieve this? If it is impossible, why?
Furthermore, can the example tree on the textbook be built when other operations are allowed?
I figured it out! The following sequence is fine:
13 11 7 2 50 80
The idea is that the tree goes unbalanced when the sequence is descending. For example,
4 3 2 1
builds an unbalanced tree
1
/
2
/
3
/
4

Complexity of creating an adjacency list from a lits of edges?

I'm creating an algorithm that can build an adjacency list from a list of edges.
For example, if the data input was:
1 2
1 8
2 8
3 5
3 1
4 5
4 6
5 2
5 9
6 4
6 8
7 4
7 10
8 4
8 6
9 4
9 5
10 7
10 3
The output would be:
1: 8 4 6
2: 4 6
3: 9 2 8
4: 2 9 8
5: 8 4
6: 5 4
7: 5 6 3
8: 5 6 4
9: 5 6 2
10: 4 5 1
The algorithm is obviously bounded by the number of vertices and edges so originally I was thinking it would be O(v + e). But I could only get the program to work by implementing for loops inside for loops with 2d arrays, which I believe cause complexity of O(N^2).
Can anyone help me better understand?
It depends a fair bit on what sort of data structure you are using to store the map from vertices to lists of adjacent vertices. Iterating through the list of edges is of course going to have time complexity O(e). Any larger time complexity is going come from the time required to find a vertex in the map and the time required to insert a new item into a vertex's list of adjacent vertices. If you were using flat arrays then you could have O(v*e) complexity (for each edge, loop through the vertex list to find the desired vertex), but this could be improved quite a lot by using a hash-table or tree data structure that gave you better lookup performance.

number of possible sudoku puzzles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
Wiki http://en.wikipedia.org/wiki/Mathematics_of_Sudoku says Sudoku has 6,670,903,752,021,072,936,960 possible permutations.I tried to find out but it seems difficult.Can someone tell me how this number is calculated.
You can find all about it in this Wiki: http://en.wikipedia.org/wiki/Mathematics_of_Sudoku.
"the number of valid Sudoku solution grids for the standard 9×9 grid was calculated by Bertram Felgenhauer and Frazer Jarvis in 2005 to be 6,670,903,752,021,072,936,960 . This number is equal to 9! × 722 × 27 × 27,704,267,971, the last factor of which is prime. The result was derived through logic and brute force computation."
You can read the most recent rewrite of the original publication by Bertram Felgenhauer and Frazer Jarvis : Mathematics of Sudoku, it details the computation over 7 pages. The calculation actually isn't trivial (the idea being to enumerate distinct and valid Sudoku grids, rather than all possible arrangements of digits over a 9x9 grid).
Interestingly there was an estimation of the number of possible sudokus posted in an internet forum before the actual value was calculated and published by Felgenhauer & Jarvis.
The author of the post points out that there are some unproven assumptions in his guess. But the estimated value differs by 0.2% from the actual value published later.
In this Wiki you can find some estimation of other types of sudoku based on similar guesses.
Here is the full post from The New Sudoku Players' Forum:
by Guest » Fri Apr 22, 2005 1:27 pm
Lets try this from a whole different direction:
Step A:
Pretend that the only 'rule' was the 'block' rule, and that the row and column rules did not exist. Then each block could be arranged 9! ways, or 9!^9 ways to populate the puzzle (1.0911*10^50 'solutions').
Step B1:
If we then say 'let us add a rule about unique values in a row', then the top three blocks can be filled as follows:
Block 1: 9! ways
Block 2: 56 ways to select which values go in each 3-cell row, and 3! ways to arrange them (remember that we haven't invented a column rule yet).
Block 3: with 1 and 2 filled, the values that go in each row is now defined, but each row can be arranged 3! ways.
Therefore, we have 9! * 56 * 3!^6 ways to fill the top three blocks, and this value cubed to fill all nine blocks. (or 8.5227*10^35 solutions). Note that this represents a 'reduction ratio' (denoted as R) of 1.2802*10^14, by adding this one new rule.
Step B2: But we could have just as easily added a 'unique in columns' rule, and achieved the same results downward instead of across, with the same value of R.
Step C: (and here is where my solution is not rigorous) What if we assume that each of these rules would constrain the number of valid solutions by exactly the same ratio? Then there would be a combined reduction ratio of R^2. So the intitial value of 1.0911*10^50 solutions would reduce by a factor of R^2, or 1.639*10^28, leaving 6.6571*10^21 valid solutions.
This post and the account are attributed to Kevin Kinfoil (Felgenhauer & Jarvis).
Additional notes
Assume the Block 1 is
1 2 3
4 5 6
7 8 9
Then we have the following possibilities for Block2, if we ignore the order of the rows
1 2 3 4 5 6
4 5 6 7 8 9
7 8 9 1 2 3
this is 1 possibility
1 2 3 7 8 9
4 5 6 1 2 3
7 8 9 4 5 6
this is 1 possibility
1 2 3 two of 4,5,6, one of 7,8,9 3*3
4 5 6 the two remaining of 7,8,9, one of 1,2,3 3
7 8 9 the two remaining of 1,2,3, the remaining of (two of 4,5,6) 1
these are (3*3)*3*1=27 possibilities
1 2 3 two of 7,8,9, one of 4,5,6 3*3
4 5 6 two of 1,2,3, the remaining of 7,8,9 3
7 8 9 the two remaining of 4,5,6, the remaining of two of 1,2,3 1
these are (3*3)*3*1=27
So all in all these are 1+1+27+27=56 possibilities.

directed graph with minimum number of chains

I have a problem but I can't figure out a solution. It goes like this:
I have a directed graph with N nodes and M links and without cycles. I need to find out the minimum numbers of chains so every node belongs to only one chain.
Example:
7 11 7 nodes; 11 links
1 2
1 5
2 3
2 5
2 7
3 4 // link exists between 3 and 4
3 6
4 6
5 4
5 6
7 3
Answer is: 2
An example is
Chain: 2-7-3-6
Chain: 1-5-4
Thanks.
He doesn't need to know if the graph is hamiltonian - knowing that it's a DAG is enough. It's probably a contest or online judge problem? It does look too hard to be homework.
Solution here: http://www2.cs.science.cmu.ac.th/person/rogaway/ps3-solns.pdf
To find the matching efficiently, consider the Hopcroft Karp algorithm: http://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm

Resources