Graph Theory37 sections · 1633 units
Open in Course

Star Graph - Implementation

Writing the Code

Here is the solution:

function findCenter(edges):
    u1 = edges[0][0]
    v1 = edges[0][1]
    u2 = edges[1][0]
    v2 = edges[1][1]

    if u1 == u2 or u1 == v2:
        return u1
    return v1

This is an O(1)O(1) solution. Fast and smart. Space: O(1)O(1).