CPSC 221

Graphs: Definitions and Implementation

Today

Announcements

  • Examlet 8 is Thursday to Sunday: hashing, priority queues, heaps
  • Lab Heaps is due Sunday 11/29
  • HW3 is due Monday 12/07
  • Friday: video lecture, disjoint sets (on PEX9 and Examlet 9)


Today: Graphs, continued

  • Finish definitions
  • Implementation: adjacency matrix and adjacency list

Collaboration Graph

Graph (V,E):

   V:   student PA3 submissions

   E:   \((u,v)\) if \(u\) is similar to \(v\)

   Aux:   vertex labelled by term

 

Sparse Graph:

\(|E| = O(|V|)\)

Downtown Vancouver

Graph (V,E):

   V:   ___________________

   E:   ___________________

 

Directed Graph:
each edge \((u,v)\) is ordered: \((u,v) \neq (v,u)\).
Howe (↓) and Seymour (↑) make this directed.

Weighted Graph:
each edge carries a weight \(w(u,v)\):
here, travel time in minutes.

In/Out-degree(v):
# edges directed into / out of \(v\).

Fastest route:
Seymour & Georgia → Burrard & Helmcken?

Graph Vocabulary

b a c e d g f n m o k l j i h p q 8 9 6 7 4 5 2 3 0 1 G = (V,E) |V| = n |E| = m
  1. List the edges incident on vertex b:
  2. What is the degree of vertex h?
  3. List all vertices adjacent to vertex i:
  4. Describe a path from p to o:
  5. Describe a path from 6 to g:
  6. List the vertices in the largest complete subgraph in G:
  7. Describe the largest connected subgraph in G:
  8. Describe the connected components in G:
  9. How many edges in a spanning forest of G?
  10. How many paths from 0 to 9?
  11. Can you draw G with no crossing edges?

Bounds

\(G = (V,E) \qquad |V| = n \qquad |E| = m\)

Running times are often reported in terms of \(n\),
the number of vertices, but they often depend
on \(m\), the number of edges.

 

Suppose \(n = 100\) vertices…

 

Give a lower bound on \(|E|\):   ______

 

Give an upper bound on \(|E|\):   ______

Handshaking

\(G = (V,E) \qquad |V| = n \qquad |E| = m\)

There are 28 vertices,
and every vertex has degree 3.

 

How many edges?

 

 

 

In general:

Activity: Pencil Puzzles

You each have a graph. Do these three things:

  1. Try to trace every edge exactly once without lifting your pencil.

  2. Come up with a rule that tells you if a trace is possible.

  3. Devise an algorithm that will find a trace if it exists.

What’s left?

How do we get from here to there?
Need:

1. Common Vocabulary

2. Graph implementation

3. Traversal

4. Algorithms.

Graph ADT

ac bd uv wz

Data:

  • Vertices
  • Edges
  • + some structure that captures connectivity

Functions (a few of them):

insertVertex(v)

removeVertex(v)

insertEdge(v, w)

removeEdge(v, w)

incidentEdges(v)

areAdjacent(v, w)


\(n\) vertices, \(m\) edges. Simple graphs by default.

Adjacency Matrix

ac bd uv wz
u v w z
u a c
v a b
w c b d
z d

An \(n \times n\) table. Cell \((v,w)\) holds the edge
between \(v\) and \(w\), or is empty.

 

Space:   ______

insertVertex(v)   ______

removeVertex(v)   ______

incidentEdges(v)   ______

areAdjacent(v, w)   ______

Adjacency List

ac bd uv wz
u→a→c
v→a→b
w→c→b→d
z→d

Each vertex keeps a list of its incident edges.
Each edge knows its two endpoints, and
where it sits in both of their lists.

 

Space:   ______

insertVertex(v)   ______

removeVertex(v)   ______

incidentEdges(v)   ______

areAdjacent(v, w)   ______

Running Times

Adj Matrix Adj List
Space \(n^2\) \(n + m\)
insertVertex(v) \(n\)* \(1\)
removeVertex(v) \(n\)* \(\deg(v)\)
insertEdge(v, w) \(1\) \(1\)
removeEdge(v, w) \(1\) \(\min(\deg(v), \deg(w))\)
incidentEdges(v) \(n\) \(\deg(v)\)
areAdjacent(v, w) \(1\) \(\min(\deg(v), \deg(w))\)

* amortized: the matrix has room to grow, like a dynamic array.

Which One?

Sparse graph: \(m = O(n)\)

Suppose \(n = 10^6\) vertices,
each with about 10 neighbours.

 

Matrix cells:   ______

List entries:   ______

Dense graph: \(m = \Theta(n^2)\)

Space:   ______   vs   ______

 

Most graphs we meet are sparse.
The adjacency list is the usual choice.

 

Next: traversal. How long does it take to visit every vertex and edge?