Graphs Flashcards

1
Q

Name for the elements in a Graph

A

Node or Vertex (nodes, vertices)

Let V={v1,v2,…}, the set of vertices
|V| = n (number of vertices)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Name for the connection btwn two elements in a Graph

A

Edges/Links

Let E={e1,e2,…} or {(v1,v2),(v4,v7),…}

|E| = m

  • can be directed or undirected
  • Can be weighted w/ a strength value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s an Adjacency Matrix

A

For a graph with n = |V| vertices,

An n×n matrix
Each cell is the relationship of the column vertex and row vertex ñ
* holds 1 (if there’s an edge) or 0 (if not)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the performance of an Adjacency Matrix

A

For a graph with n vertices:

  • Takes up O(n^2) space

Speed:

  • O(1) to check if an edge exists
  • O(1) to create an edge btwn two vertices

Takes a lot of space, but is very fast

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the performance of an Adjacency List

A

For a graph with n vertices:

  • Takes up O(m) space (m = # of edges)
  • Worst-case is O(m^2) but this rarely happens

(Note: n < m < n^2)

Speed:

  • O(n) to check if an edge exists
  • O(n) to create an edge btwn two vertices (b/c you need to check if one already exists)

Takes less space, but is slower

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s an Adjacency List

A

An array of pointers (linked list inside each)
Each index is for a node
Linked list inside stores the neighbors (other nodes with edges)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When are two nodes Adjacent

A

When they’re connected by an edge

a – b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When are two edges adjacent

A

If they have a vertex in common

a – b – c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Path

A

A sequence of adjscent edges from one vertex to another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the small world property?

A

In most networks, the shortest path between two nodes tend to be pretty short

(If there is a path btwn them, its usually short)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s a Component / connected component

A

“A maximal set of vertices such that there exists a path btwn every pair of vertices in the set”

Aka: the largest set of vertices where they all connect

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Breadth First Search (BFS) work

A
  1. Set all vertices to dist(u) = infinity
  2. Set current vertex to dist(u) = 0
  3. Put current vertex into a queue
  4. While queue isn’t empty:
    a) take curr node out of queue
    b) for all neighbors (v) of curr node (u)
    1) if v hasNot been processed yet (dist=infinity)
    a) put v into the queue
    b) make dist(v) = dist(u) + 1

Explicitely uses a queue

WILL FIND THE SHORTEST PATH

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Breadth First Search (BFS) runtime

A

O(m + n)

m = # of vertices
n = # of nodes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does Depth First Search (DFS) work

A

For a node:

  1. Set visited(u) = true
  2. For each neighbor (v) of curr node (u)
    a) if not visited, explore that node

Implicitely uses a Stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly