test case generation

how to generate test cases for problem related to graph or grid with specified condition.
for example for graph related problem i have to generate diffrent connected component of graph.
just like GALACTIC problem in JULY contest.
in grid related problem find a given sum S and that should be exist in random generated grid…