| 1 | 1 | export const degreesOfSeparation = (familyTree, personA, personB) => { |
| 2 | | throw new Error('Remove this line and implement the function'); |
| 2 | // Edge Case: Same person as both input nodes |
| 3 | if (personA === personB) { |
| 4 | return 0; |
| 5 | } |
| 6 | |
| 7 | // Edge Case: Empty family tree data |
| 8 | if (!familyTree || Object.keys(familyTree).length === 0) { |
| 9 | return -1; |
| 10 | } |
| 11 | |
| 12 | // Build adjacency list representation of the family tree |
| 13 | const graph = new Map(); |
| 14 | |
| 15 | // Add all people to the graph |
| 16 | for (const [parent, children] of Object.entries(familyTree)) { |
| 17 | if (!graph.has(parent)) { |
| 18 | graph.set(parent, []); |
| 19 | } |
| 20 | |
| 21 | for (const child of children) { |
| 22 | if (!graph.has(child)) { |
| 23 | graph.set(child, []); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // Create connections between parents and children |
| 29 | for (const [parent, children] of Object.entries(familyTree)) { |
| 30 | for (const child of children) { |
| 31 | // Parent to child connection |
| 32 | graph.get(parent).push(child); |
| 33 | // Child to parent connection |
| 34 | graph.get(child).push(parent); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Create sibling connections |
| 39 | for (const [parent, children] of Object.entries(familyTree)) { |
| 40 | for (let i = 0; i < children.length; i++) { |
| 41 | for (let j = i + 1; j < children.length; j++) { |
| 42 | const child1 = children[i]; |
| 43 | const child2 = children[j]; |
| 44 | |
| 45 | // Add sibling connection if not already connected |
| 46 | if (!graph.get(child1).includes(child2)) { |
| 47 | graph.get(child1).push(child2); |
| 48 | } |
| 49 | if (!graph.get(child2).includes(child1)) { |
| 50 | graph.get(child2).push(child1); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Edge Case: Individuals not present in the family tree |
| 57 | if (!graph.has(personA) || !graph.has(personB)) { |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | // BFS to find shortest path |
| 62 | const queue = [[personA, 0]]; // [person, distance] |
| 63 | const visited = new Set([personA]); |
| 64 | |
| 65 | while (queue.length > 0) { |
| 66 | const [currentPerson, distance] = queue.shift(); |
| 67 | |
| 68 | // Check all connected people |
| 69 | for (const neighbor of graph.get(currentPerson)) { |
| 70 | // Edge Case: Direct parent-child relationships |
| 71 | if (neighbor === personB) { |
| 72 | return distance + 1; |
| 73 | } |
| 74 | |
| 75 | if (!visited.has(neighbor)) { |
| 76 | visited.add(neighbor); |
| 77 | queue.push([neighbor, distance + 1]); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Edge Case: Disconnected individuals (no relationship) |
| 83 | return -1; |
| 84 | |
| 85 | // Handled Edge Cases: Same person as both input nodes, Empty family tree data, |
| 86 | // Individuals not present in the family tree, Direct parent-child relationships, |
| 87 | // Disconnected individuals (no relationship) |
| 3 | 88 | }; |