Google Interview Question
SDE1sCountry: India
Not sure I understand what you mean by "edges don't intersect and they seem ordered". Can you clarify? What happens for example in a complete graph of 4 nodes?
Using breadth-first search here:
public void drawGraph(node n, graphics g) {
if (n == null)
return;
Queue<Node> q = new Queue<Node>();
g.drawSmallCircle(n.point);
n.visted = true;
q.enQueue(n);
while (!q.isEmpty()) {
n = q.deQueue();
for (Node a : n.adjNodes) {
if (!a.visted) {
g.drawSmallCircle(a.point);
g.drawLine(n.point, a.point);
a.visted = true;
q.enQueue(a);
}
}
}
}
Question is not clear
- Brandy May 29, 2014