我将附加我的不同实现。 [附加为链接以更好地突出显示语法] 1. Graph_AdjMat --> graph with matrix implementation
主要需要检查代码(1)(2)&(3)
as in code (1) : implementation using matrix. [[[ CONSTANT time for checking if two node's are connected ]]]
in code(2) . : implementation using List of Lists [[[ --> here it takes O(N) for checking if two node's are connected ]]]
in code(3) . : implementation using List of Maps [[[ --> CONSTANT time for checking if two node's are connected ]]]
因此,如果我在所有这些中都实现dfs和bfs,那么DFS和BFS的时间和空间复杂度将是多少?
TIME (DFS) TIME (BFS) SPACE (DFS) SPACE (BFS)
--------------------------------------------------------------------------------
Code 1 : | | | | |
------------|------------------|------------------|-------------|--------------|
Code 2 : | | | | |
------------|------------------|------------------|-------------|--------------|
Code 3 : | | | | |
--------------------------------------------------------------------------------