Unit 6 graphs
Unit 6 graphs
Algorithms
Graphs I:
Representation and Search
Gal A. Kaminka
Computer Science Department
1
Outline
Reminder: Graphs
Directed and undirected
Matrix representation of graphs
Directed and undirected
Sparse matrices and sparse graphs
Adjacency list representation
2
Graphs
Tuple <V,E>
V is set of vertices
E is a binary relation on V
Each edge is a tuple < v1,v2 >, where v1,v2 in V
|E| =< |V|2
3
Directed and Undirected Graphs
Directed graph:
< v1, v2 > in E is ordered, i.e., a relation (v1,v2)
Undirected graph:
< v1, v2 > in E is un-ordered, i.e., a set { v1, v2 }
Degree of a node X:
Out-degree: number of edges < X, v2 >
In-degree: number of edges < v1, X >
Degree: In-degree + Out-degree
In undirected graph: number of edges { X, v2 }
4
Examples of graphs
5
Paths
Path from vertex v0 to vertex vk:
A sequence of vertices < v0, v1, …. vk >
For all 0 =< i < k, edge < vi, vi+1 > exists.
Path is of length k
Two vertices x, y are adjacent if < x, y > is an edge
Path is simple if vertices in sequence are distinct.
Cycle: if v = v
0 k
< v, v > is cycle of length 1
6
Connected graphs
Undirected Connected graph:
For any vertices x, y there exists a path xy (= yx)
Directed connected graph:
If underlying undirected graph is connected
Strongly connected directed graph:
If for any two vertices x, y there exist path xy
and path yx
Clique: a strongly connected component
|V|-1 =< |E| =< |V|2
7
Cycles and trees
Graph with no cycles: acyclic
Directed Acyclic Graph: DAG
Undirected forest:
Acyclic undirected graph
Tree: undirected acyclic connected graph
one connected component
8
Representing graphs
Adjacency matrix:
When graph is dense
|E| close to |V|2
Adjacency lists:
When graph is sparse
|E| << |V|2
9
Adjacency Matrix
Matrix of size |V| x |V|
Each row (column) j correspond to a distinct vertex j
“1”in cell < i, j > if there is exists an edge <i,j>
Otherwise, “0”
10
Examples
1 2 1
3 2 3
4
1 2 3 1 2 3 4
1 0 0 1 1 0 1 1 0
2 0 1 0 2 1 0 0 0
3 1 1 0 3 1 0 0 0
4 0 0 0 0 11
Adjacency matrix features
Storage complexity: O(|V|2)
But can use bit-vector representation
Undirected graph: symmetric along main diagonal
AT transpose of A
Undirected: A=AT
In-degree of X: Sum along column X O(|V|)
Out-degree of X: Sum along row X O(|V|)
Very simple, good for small graphs
Edge existence query: O(1)
12
But, ….
Many graphs in practical problems are sparse
Not many edges --- not all pairs x,y have edge xy
Matrix
representation demands too much memory
We want to reduce memory footprint
Use sparse matrix techniques
13
Adjacency List
An array Adj[ ] of size |V|
Each cell holds a list for associated vertex
Undirected graphs:
Each edge is represented twice
14
Examples
1 2 1
3 2 3
4
1 2 3
1 3
2 1
2 2
3 1
3 12
4
15
Node structure of Graph
struct node
{
int vertex;
struct node* next;
};
struct Graph
{
int numVertices;
struct node** adjLists;
};
class Graph
{
int numVertices;
list<int> *adjLists;
public:
Graph(int V);
void addEdge(int src, int dest);
}; 16
Adjacency list features
Storage Complexity:
O(|V| + |E|)
In undirected graph: O(|V|+2*|E|) = O(|V|+|E|)
Edge query check:
O(|V|) in worst case
Degree of node X:
Out degree: Length of Adj[X] O(|V|) calculation
In degree: Check all Adj[] lists O(|V|+|E|)
Can be done in O(1) with some auxiliary information!
17
Graph Traversals (Search)
We have covered some of these with binary trees
Breadth-first search (BFS)
Depth-first search (DFS)
A traversal (search):
An algorithm for systematically exploring a graph
Visiting (all) vertices
Until finding a goal vertex or until no more vertices
Only for connected graphs
18
Breadth-first search
19
BFS: Level-by-level traversal
Given a starting vertex s
Visit all vertices at increasing distance from s
Visit all vertices at distance k from s
Then visit all vertices at distance k+1 from s
Then ….
20
BFS in a binary tree (reminder)
BFS: visit all siblings before their descendents
2 8
1 3 6 10
7 9
5 2 8 1 3 6 10 7 9
21
BFS(tree t)
1. q new queue
2. enqueue(q, t)
3. while (not empty(q))
4. curr dequeue(q)
5. visit curr // e.g., print
curr.datum
6. enqueue(q, curr.left)
7. enqueue(q, curr.right)
Example.
23
Queue: A
A
B E
G C D
24
Queue: A B E
A
B E
G C D
25
Queue: A B E C G D F
A
B E
G C D
B E
G C D
B E
G C D
29
BFS(graph g, vertex s)
1. unmark all vertices in G
2. q new queue
3. mark s
4. enqueue(q, s)
5. while (not empty(q))
6. curr dequeue(q)
7. visit curr // e.g., print its data
8. for each edge <curr, V>
9. if V is unmarked
10. mark V
11. enqueue(q, V) 30
The general BFS algorithm
Each vertex can be in one of three states:
Unmarked and not on queue
Marked and on queue
Marked and off queue
The algorithm moves vertices between these
states
31
Handling vertices
Unmarked and not on queue:
Not reached yet
Marked and on queue:
Known, but adjacent vertices not visited yet (possibly)
Marked and off queue:
Known, all adjacent vertices on queue or done with
32
Queue: A
A
B E
G C D
33
Queue: A B E
A
B E
G C D
B E
G C D
35
Queue: A B E C G D F
A
B E
G C D
Do same with E.
36
Queue: A B E C G D F
A
B E
G C D
Visit C.
Its neighbor F is already marked, so not queued.
37
Queue: A B E C G D F
A
B E
G C D
Visit G.
38
Queue: A B E C G D F
A
B E
G C D
39
Queue: A B E C G D F
A
B E
G C D
Visit F.
E, D, C marked, so not queued again.
40
Queue: A B E C G D F
A
B E
G C D
42
Depth-first search
43
DFS(graph g, vertex s)
Assume all vertices initially unmarked
DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)
}
44
Current vertex: A
A
B E
G C D
45
Current: B
A
B E
G C D
B E
G C D
47
Current: F
A
B E
G C D
Visit F.
Pick one of its neighbors, E.
48
Current: E
A
B E
G C D
B E
G C D
B E
G C D
4
F
53