Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 10 Notes: Adjacency Lists + Cow Contest #11

Open
varmichelle opened this issue Nov 14, 2016 · 0 comments
Open

Week 10 Notes: Adjacency Lists + Cow Contest #11

varmichelle opened this issue Nov 14, 2016 · 0 comments

Comments

@varmichelle
Copy link
Owner

varmichelle commented Nov 14, 2016

Adjacency matrix: (N*N with space for each node-node pair)
Pros: easy to use, linear access time
Cons: takes up a lot of memory

Adjacency list: (N with list of neighbors for each node)
If graph with no weights (or equal weights), just store a list of neighbors
If graph with weights, store a list of (neighbor, distance) pairs
Pros: less memory
Cons: more difficult to use, slower
Could use array of vectors, vector of vectors, array of maps, array of sets, array of linked lists
Array of maps is easiest bc you can directly access graph[a][b] (in a similar way)
Example:
Map<int,boolean> (no weights or equal weights) or <int,int> (weights) graph[N];
if (graph[a][b]==true) or if (graph[a][b] != INF) // then a and b are connected
// note: if map is empty, accessing the value should give the default value (false for boolean, 0 for int)

When to use which:
Use adjacency list for graphs with relatively few edges, can be many vertices (sparse graphs)
Use adjacency matrix for graphs with relatively few vertices, can be many edges (dense graphs)

Solution to cow contest:
Construct a directed graph with paths from winner to loser
Run Floyd-Warshall
for (int c = 0; c < N; c++)
boolean answer = true;
for (int d = 0; d < N; d++)
if (d == c) continue
if (graph[c][d] == INF && graph[d][c] == INF
answer = false
if (answer) count++
print count

@varmichelle varmichelle changed the title Week 10 Notes: Adjacency Lists Week 10 Notes: Adjacency Lists + Cow Contest Nov 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant