Graphs
Graphs
What is a graph?
◼ A data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes
to each other
◼ The set of edges describes relationships among
the vertices
Formal definition of graphs
● An undirected graph is
one in which the pair of
vertices in a edge is
unordered, (v0, v1) =
(v1,v0)
Directed vs. undirected graphs
(cont.)
◼ When the edges in a graph have a direction,
the graph is called directed (or digraph)
• Complete Graph
0 • V(G1)={0,1,2,3}
• E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)
1 2 }
3 0 0
G 1 2 2 1
1
3 3
subgraphs of G1
Graph terminology (cont.)
Connected Graphs
0 0
1 2 1 2
3
3 4 5 6
G
1 G
2
tree (acyclic graph)
Graph terminology (cont.)
● A connected component of an undirected graph
is a maximal connected subgraph.
H H
0 4
1 2
2 1 5
3 6
7
G4 (not
connected)
● A tree is a graph that is connected and acyclic.
Graph terminology (cont.)
0
0 2
1
2
G
3
Graph terminology (cont.)
● The degree of a vertex is the number of edges
incident to that vertex
● For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
– if di is the degree of a vertex i in a graph G with n
vertices and e edges, the number of edges is
Graph terminology (cont.)
Degree
undirected graph
3 0
0 2
G 1 2
G 3 1 23 2 3 3
1
33 3 4 5 6
1 1 1 1
in:1, out:
0
directed graph 1
in: 1, out: in-degree
1 2 out-degree
in: 1, out:
2 0
G
Graph Representation
1
0
0 1 0 1 0
A= 0 0 1 0 0
2
0 0 1 0 0
1 0 0 0 0 4
0 0 0 4 0 3
Another Example of Adj. Matrix
◼ Cons:
▪ No matter how few edges the graph has, the matrix
takes O(n2) in memory
Adjacency Lists Representation
◼ Pros:
▪ Saves on space (memory): the representation takes as many
memory words as there are nodes and edge.
◼ Cons:
▪ It can take up to O(n) time to determine if a pair of nodes
(i,j) is an edge: one would have to search the linked list L[i],
which takes time proportional to the length of L[i].
Graph Implementation
◼ Array-based implementation
▪ A 1D array is used to represent the vertices
▪ A 2D array (adjacency matrix) is used to
represent the edges
Array-based implementation
Examples for Adjacency Matrix
0 0 4
0
2 1 5
1 2
3 6
3 1
7
2
G
2
G1
symmetri
undirected: c
n2/2
directed: n2
G
Graph implementation (cont.)
◼ Linked-list implementation
▪ A 1D array is used to represent the vertices
▪ A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Linked-list implementation
Adjacency matrix vs. adjacency list
representation
0
1 2
3
Adjacency Matrix G Adjacency List
1
0 1 2 3
1 0 2 3
2 0 1 3
3 0 1 2
Adjacency matrix vs. adjacency list
representation
◼ Adjacency matrix
▪ Good for dense graphs --|E|~O(|V|2)
▪ Memory requirements: O(|V| + |E| ) = O(|V|2 )
▪ Connectivity between two vertices can be tested quickly
◼ Adjacency list
▪ Good for sparse graphs -- |E|~O(|V|)
▪ Memory requirements: O(|V| + |E|)=O(|V|)
▪ Vertices adjacent to another vertex can be found quickly
Graph searching
◼ Problem: find a path between two nodes of the
graph (e.g., Austin and Washington)
DFS Tree
Implementation of DFS
DFS(input: Graph G)
1. Stack S;
2. while (G has an unvisited node x)
3. visit(x);
4. push(x,S);
5. while (S is not empty)
6. t := S(top)
7. if (t has an unvisited neighbor y)
visit(y);
8. push(y,S);
9. else
10. pop(S);
11.
Depth-First-Search (DFS)
(initialization)
Breadth-First-Searching (BFS)
◼ What is the idea behind BFS?
▪ Look at all possible paths at the same depth
before you go at a deeper level
▪ Back up as far as possible when you reach a
"dead end" (i.e., next vertex has been "marked"
or there is no next vertex)
Illustration of BFS
0 0 1
2 4 2
1
9 4
5 9 10 5 7
10 8
11 11 6
6 7 8
Graph G
BFS Tree
BFS pseudocode
function bfs(v):
print ‘v’
enqueue{v}. a b c
mark v as visited.
d e f
while queue is not empty:
g h
u := dequeue().
for each unvisited neighbor w of v:
enqueue(w).
mark w as visited.
▪ Trace bfs(a, f) in the above graph.
▪ BFS always returns the shortest path (the one with the fewest
edges) between the start and the end vertices.
Runtime of DFS and BFS
◼ What is the expected runtime of DFS and BFS, in terms of
the number of vertices V and the number of edges E ?
(initialization)
next:
Applications of Graphs
◼ World Wide Web, web pages are considered to be the vertices.
There is an edge from a page u to other page v if there is a link
of page v on page u. This is an example of Directed graph.
◼ In Computer science graphs are used to represent the flow of
computation.
◼ Communication Networks, vertices in graph represent
terminals, processors and edges represent transmission channels
like wires, fibers etc. through which the data flows.
◼ In Operating System, we come across the Resource Allocation
Graph where each process and resources are considered to be
vertices. Edges are drawn from resources to the allocated
process, or from requesting process to the requested resource.