{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS Notes 14 - o Space O o Run time • Is edge(x,y in graph...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Graphs (Network) Monday, April 04, 2011 3:49 PM Storing a graph in a program 1. Adjacency matrix/table storage A B C D E A F T T F F B T F F F T C T F F T F D F F T F T E F T F T F o 2D array 5x5 o Boolean matrix o Boolean[ ][ ] M=new boolean [5][5] 1. Weighted/Directed graph o need int or float matix o True=> edge weight o False=> -1 Space? For graph with n vertices and e edges o O(n 2 ) Is there an edge (x,y) [undirected graph] in the graph? Worst case time for adjacency matrix= O(1) o Is M[x][y] true? What are all the neighbors of x? Worst case time= O(n) Best case time= O(n) Scan row x Simple graph with n verticies Max # of edges= n(n-1) 2 Dense vs Sparse Graphs O(n logn)= dividing line Storage Schemes 1. Adjacency matrix
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: o Space: O( ) o Run time • Is edge (x,y) in graph? O(1) • Find/List all neighbors of x O(n) o Appropriate for dense graphs o For sparse graphs, waste a lot of space storing false values o Instead, store the neighbors for each of the vertices in a LL 1. Adjacency LL o Space: • n+2e • # of array spots=n • # of nodes= 2e undirected E directed • O(n+e) o Run time: • Is edge (x,y) in graph? O(min(n,e)) • Find/List all neighbors of x O(min(n,e)) o Much better for sparse graphs (generally considered default since graphs are usually more sparse)...
View Full Document

{[ snackBarMessage ]}