what is the worst-case running time for finding two given vertices are adjacent in Adjacency matrix implmentation of a graph
What is the worst-case running time for finding two given vertices are adjacent in Adjacency matrix implmentation of a graph? Is that not O(1) as I know the indices of those vertices in the matrix so that I can pick the value in constant time开发者_开发知识库? I read it as O(n^2) in a book. Someone please explain it how to get to this measure.
Thanks
An adjacency matrix occupies O(n^2) memory, which may be where you're confused. But yes lookup given two vertices is O(1), that's the advantage of an adjacency matrix.
yes. it is O(1) for the reasons stated by you.
精彩评论