Graphs
For expressing non-hierarchically
related items
1
12/28/2010
Graphs
A graph consists of
A set of vertıces (nodes) (V = {v0, . . .})
A set of edges (arcs): pairs of vertices (E)
Vertices with an edge between are called adjacent.
Depending on problem, nodes or edges may have
labels (or weights)
Graphs
A path between two vertices is a sequence of
edges that begins at one vertex and ends at one.
2
12/28/2010
Graph Examples
Graphs
An undirected graph is connected if there is a path between every
pair of nodes. That is, if one node of the pair is reachable from the
other.
If a directed graph has this property, it is called strongly connected
If a directed graph becomes connected when arc directions are
neglected, it is called weakly connected.
A complete graph is a graph in which there is an edge between
every pair of vertices
A DAG is a (rooted) tree iff connected, and every node but the root
has exactly one parent.
A connected, acyclic, undirected graph is also called a free tree.
Free: we’re free to pick the root; e.g.,
3
12/28/2010
Graph – An Example
1 2 A graph G (undirected)
3 4 5
• Adjacent:
1 and 2 are adjacent -- 1 is adjacent to 2 and 2 is adjacent to 1
• Path:
1,2,5 ( a simple path), 1,3,4,1,2,5 (a path but not a simple path)
• Cycle:
1,3,4,1 (a simple cycle), 1,3,4,1,4,1 (cycle, but not simple cycle)
Representation
Adjacency Matrix
○ A two dimensional array
Adjacency List
○ For each vertex we keep a list of adjacent vertices
4
12/28/2010
Adjacency Matrix
Adjacency Matrix
10
5
12/28/2010
Adjacency Matrix
11
Adjacency List
An adjacency list for a graph with n vertices numbered
0,1,...,n-1 consists of n linked lists. The ith linked list has a
node for vertex j if and only if the graph contains an edge
from vertex i to vertex j.
Adjacency list is a better solution if the graph is sparse.
Space requirement is O(|E| + |V|), which is linear in the
size of the graph.
In an undirected graph each edge (v,w) appears in two
lists.
Space requirement is doubled.
12
6
12/28/2010
Adjacency List
13
Adjacency List
14
7
12/28/2010
Graph Traversals
A graph-traversal algorithm starts from a
vertex v, visits all of the vertices that can be
reachable from the vertex v.
15
Depth-First Traversal
Start from a given vertex. Visiting a vertex v,
the depth-first traversal algorithm visits (if
possible) an unvisited adjacent vertex to
vertex v.
16
8
12/28/2010
17
9
12/28/2010
while(!S.isEmpty()) {
Vertex current = S.pop();
if(!marked(current)) {
mark(current);
visit(current);
for each edge(current,w)
if(!marked(w)) S.push(w);
}
}
Depth-first Traversal
Stack
10
12/28/2010
Breadth-First Traversal
After visiting a given vertex v, visit every
vertex adjacent to v
You should specify the order in which it
should visit the vertices adjacent to v.
We may visit the vertices adjacent to v in sorted order.
21
22
11
12/28/2010
Topological Sorting
12
12/28/2010
Topological Sorting
13
12/28/2010
Topological Sorting
Analysis:
Finding the node with no predecessor O(|V|) // scan the array of vertices
Repeat this for V nodes O(|V|2)
1 2
3 4 5
6 7
28
14
12/28/2010
s.dist = 0;
q.enqueue(s);
while(!q.isEmpty()) {
Vertex v= q.dequeue();
30
15
12/28/2010
PriorityQueue<Vertex> pq;
For each node v { v.dist = ∞; v.path = null; }
s.dist = 0;
add all vertices to pq;
while(!pg.isEmpty()) {
Vertex v = pq.deleteMin();
for each edge(v,w)
if(v.dist + weight(v,w) < w.dist) {
update w.dist = v.dist + weight(v,w);
w.path = v;
}
}
32
16
12/28/2010
33
Analysis:
17