CS 280 - Data Structures
Introduction to Graphs
- One of the most useful data structures. (A very large topic.)
- Related to trees in that a tree is a special kind of graph (much simpler).
- Graphs are more general and have a wider range of use.
- Represent problems involving interconnected (dependent) objects.
- Graph algorithms are more complex. Need to account for cycles; trees have only one path between nodes.
- Graphs can have several (connected) components, a tree is always a single entity.
- Topological ordering, all nodes "dependent" on X are in list L before X. (Prerequistes in a school.)
- Shortest path, weighted graphs to represent travel destinations and times
- Task networks, time it takes to complete a task before starting the next one.
Terminology
- A collection of points connected by line segments.
- The points are referred to as nodes or vertices, the segments as edges.
- If the edges have a direction (arrowheads in a diagram), the graph is a directed graph
or digraph
- A graph that has values (weights or costs) assigned to the edges is called a weighted graph.
Graph
|
Directed graph (digraph)
|
Weighted graph
|
Some Notation
A graph, G, consists of a set of vertices, V, and edges, E where the edges are constructed
from pairs of distinct vertices.
G = (V, E)
In an undirected graph, each edge is an unordered pair:
e = {v1, v2}
In a directed graph, each edge is an ordered pair:
e = (v1, v2)
and v1 is the origin (source) and v2 is the terminus (destination).
- Two vertices, x and y are said to be adjacent if there is an edge connecting them.
- We use the notation sGd to mean that s is adjacent to d. With a digraph, sGd
implies direction. (xGy is not the same as yGx).
- All nodes adjacent to s is called the adjacency set of s.
Paths and Connectivity
- Sequence (contiguous) of edges is a path.
- If there is a path from x to y, y is reachable from x.
- The length of a path is the number of edges on the path. (xGx has length of 1).
- Two vertices are connected if there is a path from one to the other.
- A connected component is a subset, S, of vertices that are all connected.
- Digraphs can be strongly connected or weakly connected. (Definitions vary.)
Cycles
- A path whose source and destination node are the same.
- A self-loop is a cycle (path length of 1)
- A cycle is simple if all nodes on the path are distinct (with the exception of the first and last).
- If a graph has no cycles, it is acyclic. A directed acyclic graph is called a DAG.
Properties of Digraphs
For all nodes x in G, xGx is true:
Reflexive
|
|---|
For all nodes x in G, xGx is false:
Irreflexive
|
|---|
For some nodes x in G (but not all), xGx is true:
Neither reflexive nor irreflexive
|
|---|
For all nodes x and y in G, xGy and yGx is true:
Symmetric
|
|---|
For all nodes x and y in G, xGy and yGx is false:
Antisymmetric
|
|---|
For some nodes x and y in G (but not all), xGy and yGx is true:
Neither symmetric nor antisymmetric
|
|---|
For all nodes x, y, and z in G, if xGy and yGz then xGz:
Transitive
|
|---|
For all nodes x, y, and z in G, if xGy and yGz then not xGz:
Not transitive
|
|---|
Degree
- in-degree - number of incoming edges into a node (node is a destination)
- out-degree - number of outgoing edges from a node (node is a source)
- degree - number of edges (in/out) connecting to a node
Questions:
- How do directed and undirected graphs differ?
- What is a path in a graph? What is a cycle in a graph?
- What is a simple cycle?
- What is the in-degree and out-degree of a vertex in a directed graph?
Adjacency Matrix
- A graph G with M nodes represented by a M x M boolean array (matrix).
- For each x and y, G(x,y) = TRUE if xGy, otherwise false.
- Determining if two nodes are adjacent is O(1)
- Space required is O(M2) as well as algorithms that must access all elements
| A | | B |
 |
|
 |
Graph A:
| | 1 | 2 | 3 | 4 | 5 | 6 |
| 1 | F | T | T | T | F | F |
|---|
| 2 | T | F | F | F | T | F |
|---|
| 3 | F | F | F | F | T | F |
|---|
| 4 | F | F | F | T | F | F |
|---|
| 5 | F | F | T | F | F | T |
|---|
| 6 | F | F | F | F | F | T |
|---|
|
|
| | 1 | 2 | 3 | 4 | 5 | 6 |
| 1 | 0 | 5 | 6 | 4 | 0 | 0 |
|---|
| 2 | 0 | F | F | F | T | F |
|---|
| 3 | 0 | F | F | F | T | F |
|---|
| 4 | 4 | F | F | T | F | F |
|---|
| 5 | 0 | F | T | F | F | T |
|---|
| 6 | 3 | F | F | F | F | T |
|---|
|
Adjacency List
Traversals
- Depth-first, uses stacks
- Breadth-first, uses queues
- Describe differences between depth-first search and a bread-first search.
- How do stacks and queues relate to the search methods?