0% found this document useful (0 votes)
93 views27 pages

Kinds of Graphs. Data Structures For Graph Representation. Connectivity: The Na Ive Algorithm and Warshall Algorithm

The document discusses different ways to represent graphs and digraphs, including lists of nodes and edges, adjacency lists, and adjacency matrices. It provides examples of undirected and directed graphs and how their corresponding adjacency matrices are constructed. The adjacency matrix represents the connections between nodes in the graph and can be used to reconstruct the graphical representation.

Uploaded by

Raluca Romanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views27 pages

Kinds of Graphs. Data Structures For Graph Representation. Connectivity: The Na Ive Algorithm and Warshall Algorithm

The document discusses different ways to represent graphs and digraphs, including lists of nodes and edges, adjacency lists, and adjacency matrices. It provides examples of undirected and directed graphs and how their corresponding adjacency matrices are constructed. The adjacency matrix represents the connections between nodes in the graph and can be used to reconstruct the graphical representation.

Uploaded by

Raluca Romanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Lecture 10

Kinds of graphs. Data structures for graph representation.


Connectivity: The naı́ve algorithm and Warshall algorithm

Isabela Drămnesc UVT

Computer Science Department,


West University of Timişoara,
Romania

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 1 / 25


Remember that:
Graph = mathematical structure G = (V , E ) where
V : set of nodes (or vertices)
E : set of edges incident to 2 nodes, or 1 node
Depending on the kind of edges e ∈ E , graphs are of two kinds:
I Undirected: Every edge has one or two endpoints
e
e
a b or a (loop)
Graphical representation:
I Directed (or digraphs): Every edge e ∈ E has a source (or start)
end a destination (or end)
e

a e b or a (loop)
Graphical representation:
The directed edges are called arcs.
Simple graph: graph (directed or not) with at most one arc between any
pair of nodes, and no loop.
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 2 / 25
Common types of graphs
Taxonomy

G = (V , E )
e1 , e2 ∈ E are parallel edges if they are incident to the same nodes,
and
If G is directed, then start(e1 ) = start(e2 ) and
end(e1 ) = end(e2 )

I Multigraph (directed or not):no loops, and if the graph is


undirected: it can have parallel edges
directed: it can have parallel arcs
I Pseudograph: undirected graph that can have loops and
parallel edges.
I Weighted graph: every edge e ∈ E has a weight w (E );
usually w (e) ∈ R.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 3 / 25


Graphical representations of graphs
Illustrative examples

I Simple graphs: draw lines or arcs between the connected


nodes
b c
Simple undirected graph: e
a d
b c
Simple directed graph: e
a d
I Simple weighted graphs: we indicate the weights along the
corresponding connections
2
9
b c
12
3

a 5 1 f
4
7

1
e d
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 4 / 25
Graphical representations of graphs
Example (continued)

Multigraphs or pseudographs: if we want to distinguish parallel


edges, we can label them:
e2 e4
a
e3
e1 c d
multigraph:
e5
b e6
e2 e4 e9
a
e3
e1 c d
pseudograph:
e5 e7
b e6
e8

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 5 / 25


Concrete representations of graphs

1 List of nodes + list of edges


2 Adjacency lists
3 Adjacency matrix
4 Incidence matrix
5 Weight matrix

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 6 / 25


Simple graphs
The representation by list of nodes + list of edges

Example
b c List of nodes V = [a, b, c, d, e]
List of edges E = [{a, b}, {a, c}, {a, d}, {b, c}, {c, e}, {d, e}]
e Remarks: {a, b} = {b, a}, {a, c} = {c, a}, a.s.o.
a edge ↔ set of nodes adjacent to the edge
d
b c List of nodes V = [a, b, c, d, e]
List of arcs E = [(a, b), (c, a), (c, b), (d, a), (e, c), (e, d)]
e Remarks: (a, b) 6= (b, a), (a, c) 6= (c, a), a.s.o.
a edge ↔ pair (start,end)
d

Remark
If the graph has no isolated nodes (with 0 neighbors), there is no
need to store the list of nodes V :
I V can be computed from E

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 7 / 25


Simple graphs
The representation with adjacency lists

For every node u ∈ V we keep the list of nodes that are adjacent
to u
I If G is undirected, v is adjacent to u of there is an edge with
endpoints u and v .
In undirected graphs, the relation of adjacency is symmetric.
I If G is directed, v is adjacent to u if there is an arc e ∈ E
from u to v , i.e. start(e) = u and end(e) = v .

Example
b c a 7→ [b, c, d] d 7→ [a, e]
b 7→ [a, c] e 7→ [c, d]
e c 7→ [a, b, e]
a d
b c a 7→ [b] d 7→ [a]
b 7→ [] e 7→ [c, d]
e c 7→ [a, b]
a d
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 8 / 25
Graphs
The representation with adjacency matrix AG

If G has n nodes, AG = (mij ) has dimension n × n and


mij := number of edges from the i-th node to the j-th node.

Remarks
1 Before computing M
G from G , be must fix an enumeration of
its nodes: [v1 , v2 , . . . , vn ]
2 If G is undirected, AG is a symmetric matrix
3 If G is a simple graph, AG contains just 0s and 1s

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 9 / 25


Undirected graphs
The adjacency matrix AG of an undirected graph G

The adjacency matrix the undirected graph

b c
G: e
a d
for the node enumeration [a, b, c, d, e] is
 
0 1 1 1 0
1 0 1 0 0
 
AG = 1 1 0 0 1
1 0 0 0 1
0 0 1 1 0

Remark: the matrix AG is symmetric.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 10 / 25


Graphs
The digraph corresponding to a symmetric adjacency matrix

If A is a symmetric matrix of size n × n with aij ∈ N for all i, j, an


undirected graph G whose adjacency matrix is A can be built as
follows:
1 Draw n points v1 , . . . , vn in plane
2 For every i, j ∈ {1, . . . , n}, draw aij distinct edges between vi
and vj

Example
 
0 1 0 2 v2 v3
1 0 1 0

A= 0 ⇒G :
1 0 1
2 0 1 0 v1 v4

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 11 / 25


Digraphs
The adjacency matrix AG of a digraph G

The adjacency matrix of the digraph

b c
G: e
a d
for the enumeration [a, b, c, d, e] of nodes is
 
0 1 0 0 0
0 0 0 0 0
 
AG =  1 1 0 0 0 
1 0 0 0 0
0 0 1 1 0

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 12 / 25


Digraphs
The digraph corresponding to an adjacency matrix

If A is an n × n matrix with aij ∈ N for all i, j, a digraph G whose


adjacency matrix is A can be constructed as follows:
1 Draw n points v1 , . . . , vn in the plane
2 For every i, j ∈ {1, . . . , n}, draw aij distinct arcs from vi to vj

Example

 
0 1 0 2 v2 v3
0 0 1 0

A=
0 ⇒G :
1 1 1
1 0 0 0 v1 v4

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 13 / 25


Digraphs with labeled edges
The representation with incidence matrices

We assume given two lists (or enumerations):


V = [v1 , . . . , vn ] of the nodes of G
L = [e1 , . . . , ep ] of the labels of edges of G
The incidence
 matrix MG = (mij ) has dimension n × p and
 −1 if start(ej ) = vi
mij = 1 if end(ej ) = vi
0 otherwise.

Example
If V = [a, b, c, d, e], L = [e1 , e2 , e3 , e4 , e5 , e6 , e7 , e8 ] and
−1 0 0 −1 1 0 0 −1
 b
e2
1 −1 0 0 0 1 0 0 e1
MG =  −1 −1 ⇒ e4
 0 1 1 0 0 0

a c

e6
e5
0 0 1 0 0 0 1 1 e8
−1 −1

e3
0 0 0 0 0 0
e e7 d

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 14 / 25


Simple weighted graphs
The representation with weight matrix

The weight matrix WG = (wij ) of a simple weighted graph G with


n nodes [v1 , . . . , vn ] has dimension n × n and
B wii = 0 for all 1 ≤ i ≤ n.
B wij = w ({vi , vj }) for every edge {vi , vj } ∈ E , if G is
undirected.
B wij = w ((vi , vj )) for every arc (vi , vj ) ∈ E , if G is directed.
B wij = ∞ otherwise.

Example (Weight digraph with node enumeration [a, b, c, d, e, f ])


2
9 0 3 ∞ ∞ 4 ∞
b c
3 ∞ 0 2 ∞ ∞ ∞
12

∞ 9 0 ∞ 1 12
G :a f ⇒ WG = 
1

5 
4 ∞ ∞ ∞ 0 ∞ 7
7

1 ∞ 5 ∞ 1 0 ∞
e d ∞ ∞ ∞ ∞ ∞ 0

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 15 / 25


The representation of graphs
Comparative study

I The representation with list of edges


Adequate for the representation of simple graphs without
isolated nodes, and with |E |  |V |
Space complexity: O(|E |)
I The representation with lists of adjacencies
Allows the fast enumeration of the neighbors of a node
Space complexity: O(|V | + |E |)
I The representation with adjacency matrix AG = (aij ) or with
weight matrix WG = (wij )
Fast test to check direct connection between 2 nodes: O(1)
@(vi , vj ) ∈ E if aij = 0 or if wij = ∞
Space complexity: O(|V |2 )
- inadequate representation when |E |  |V |2
The representation with incidence matrix MG
I Time complexity: O(|V | · |E |)

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 16 / 25


Simple digraphs
Properties of the adjacency matrix AG

We assume that G is a simple (di)graph with n nodes and with the


adjacency matrix AG = (aij )
aij = 0 (or false) if @ arc vi → vj
aij = 1 (or true) if ∃ arc vi → vj
We define:
In : the identity matrix of size n × n
The Boolean operations (conjunction) and ⊕ (disjunction):
0 1 ⊕ 0 1 Remarks:
0 0 0 0 0 1 a b = min(a, b)
1 0 1 1 1 1 a ⊕ b = max(a, b)
If U = (uij ), V = (vij ) are n × n matrices with elements 0 or
1, we define
U ⊕ V = (cij ) if cij = uij ⊕ vij for all i, j
U V = (dij ) if dij = (ui1 v1j ) ⊕ . . . ⊕ (uin vnj )
U k = U . . . U for every k > 0
| {z }
k times
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 17 / 25
Simple digraphs
Properties of the adjacency matrix AG (contd.)

Properties
(k) (k)
1 If AkG = (aij ) for k ≥ 1 then aij = 1 if and only if there is a
path with length k from node vi to vj .
2 Let A∗G = In ⊕ AG ⊕ A2G ⊕ . . . ⊕ An−1
G = (aij ). Then
aij = 1 if and only if there is a path with length
j ∈ {1, . . . , n − 1} from node vi to vj .
A∗G can be computed in O(n4 ).
A∗G is called reflexive and transitive closure of AG .
3 vi and vj are connected ⇔ ∃ a simple path vi vj ⇔ aij = 1.
4 In ⊕ AG ⊕ A2G ⊕ . . . ⊕ Ak+1
G = In ⊕ (In ⊕ AG ⊕ A2G ⊕ . . . ⊕ AkG ) AG
⇒ A∗ can be computed in O(n4 ).

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 18 / 25


Simple digraphs
Properties of the adjacency matrix AG (contd.)

Properties
(k) (k)
1 If AkG = (aij ) for k ≥ 1 then aij = 1 if and only if there is a
path with length k from node vi to vj .
2 Let A∗G = In ⊕ AG ⊕ A2G ⊕ . . . ⊕ An−1
G = (aij ). Then
aij = 1 if and only if there is a path with length
j ∈ {1, . . . , n − 1} from node vi to vj .
A∗G can be computed in O(n4 ).
A∗G is called reflexive and transitive closure of AG .
3 vi and vj are connected ⇔ ∃ a simple path vi vj ⇔ aij = 1.
4 In ⊕ AG ⊕ A2G ⊕ . . . ⊕ Ak+1
G = In ⊕ (In ⊕ AG ⊕ A2G ⊕ . . . ⊕ AkG ) AG
⇒ A∗ can be computed in O(n4 ).

Corollary
The connectivity between all pairs of nodes in a simple digraph can
be checked in O(n4 ).
Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 18 / 25
Simple digraphs
Better ways to compute A∗G

Warshall algorithm computes A∗G in O(n3 ).

Warshall’s main idea


If V = [v1 , . . . , vn ] is an enumeration of the nodes of G and
vk ∈ V , then every simple path π : vi vj is of one of the
following two kinds:
1 vk does not appear along π between vi and vj
vk
vi vj
2 vk appears exactly once in π, between vi and vj
vk
vi vj

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 19 / 25


Simple digraphs
Warshall algorithm to compute A∗G

Assumption: AG = (aij ) has size n × n


[n]
I Compute recursively C [n] = (cij ) where
(
[k] aij if k = 0
cij := [k−1] [k−1] [k−1]
cij ⊕ (cik ckj ) if k ≥ 1

Properties
1 C [0] = AG
[k]
2 cij = 1 if and only if there is a path π : vi vj where all
intermediary nodes are from the subset {v1 , . . . , vk }
3 C [n] = A∗G
4 C [n] can be computed in O(n3 ).

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 20 / 25


Simple weighted digraphs
The lightest paths

Consider the simple digraph G = (V , E ) with V = {1, . . . , n} and


the weight function w : E → R+
I In G , the weight of a path π : v1 → v2 → . . . → vp is
Pp−1
w (π) = i=1 w ((vi , vi+1 ))
(we add up the weights of all arcs of π)
I For every pair of nodes (i, j) from V , we wish to find
a lightest path from node i to node j
(there can be more than one lightest path)
the weight of a lightest path
Remember that the weight matrix of G is WG = (wij ) where

 0 if j = i,
wij = w ((i, j)) if (i, j) ∈ E ,
∞ if (i, j) 6∈ E

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 21 / 25


Finding the lightest paths in a simple weighted digraph
Warshall algorithm: main idea

Let k ∈ V = {1, . . . , n}, and π : i j be a lightest path from i to


j. We distinguish two cases:
1 k is not an intermediary node of π
π1 π2
2 k is an intermediary node of π. Then π = i k j and
w (π) = w (π1 ) + w (π2 ).

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 22 / 25


Finding the lightest paths in a simple weighted digraph
Warshall algorithm: main idea

Let k ∈ V = {1, . . . , n}, and π : i j be a lightest path from i to


j. We distinguish two cases:
1 k is not an intermediary node of π
π1 π2
2 k is an intermediary node of π. Then π = i k j and
w (π) = w (π1 ) + w (π2 ).
An useful auxiliary data structure:
[k]
A matrix of paths P [k] = (pij ) where


 • special value: @ path i j through
intermediary nodes from {1, . . . , k}

[k]
pij :=

 π a lightest path from i to j through
intermediary nodes from {1, . . . , k}

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 22 / 25


Finding the lightest paths in a simple weighted digraph
Warshall algorithm: the recursive formula of computation

We assume that WG = (wij )



 •
 if k = 0 and wij = ∞
[k]
pij := [i, j] if k = 0 and wij < ∞
 p-min(p [k−1] , p [k−1]  p [k−1] ) if k ≥ 1

ij ik kj

[k−1] [k−1]
where pik  pkj denotes the concatenation of the paths
[k−1] [k−1]
pik and pkj . If both exist (i.e., they are not •), and •
otherwise.
Remark
[n] [n]
If pij = •, there is no path from node i to j. Otherwise, pij is a
lightest path from node i to j.

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 23 / 25


Finding the lightest paths in a simple weighted digraph
Warshall algorithm: pseudocode

Compute recursively and simultaneously two matrices:


   
[k] [k]
W [k] = wij and P [k] = pij for 0 ≤ k ≤ n
(
[k]
wij  if k = 0,
wij =

[k−1] [k−1] [k−1]
max wij , min{wik , wkj } otherwise.

[0]


 • if k = 0 and wij = ∞,
 [0]
[k]
 [i, j] if k = 0 and wij 6= ∞,
pij = [k−1] [k−1] [k−1] [k−1]


 wij if k > 0 and wij ≤ wik + wkj ,
 w [k−1]  w [k−1]

otherwise.
ik kj

Remark
W [n] and P [n] can be computed in O(n3 ).

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 24 / 25


A special case of Warshall algorithm
Finding the shortest paths in a simple digraph

All arcs are assumed to have weight 1


v2 • • [v1 , v3 ] • •
 
[v2 , v1 ] • • • • 
v1 v3 [0]
PG =  • • • • [v3 , v5 ] ,
 
 • • [v4 , v3 ] • • 
v5 v4 [v5 , v1 ] • • [v5 , v4 ] •
• • [v1 , v3 ] • •
 
[v2 , v1 ] • [v2 , v1 , v3 ] • • 
[1]
PG =  • • • • [v3 , v5 ],
 
 • • [v4 , v3 ] • • 
[v5 , v1 ] • [v5 , v1 , v3 ] [v5 , v4 ] •
• • [v1 , v3 ] • [v1 , v3 , v5 ]
 
[v2 , v1 ] • [v2 , v1 , v3 ] • [v2 , v1 , v3 , v5 ]
[2] [1] [3]  [4] (3)
PG = PG , PG =  • • • • [v3 , v5 ] , PG = PG ,

 • • [v4 , v3 ] • [v4 , v3 , v5 ] 
[v5 , v1 ] • [v5 , v1 , v3 ] [v5 , v4 ] •
[v1 , v3 , v5 , v1 ] • [v1 , v3 ] [v1 , v3 , v5 , v4 ] [v1 , v3 , v5 ]
 
 [v2 , v1 ] • [v2 , v1 , v3 ] [v2 , v1 , v3 , v5 , v4 ] [v2 , v1 , v3 , v5 ]
[5] 
PG = [v3 , v5 , v1 ] [v3 , v5 , v1 , v3 ] [v3 , v5 , v1 , v3 ] • [v3 , v5 ] 

[v , v , v , v ]
4 3 5 1 • [v ,
4 3 v ] [v , v
4 3 5 4, v , v ] [v , v
4 3 5 , v ] 
[v5 , v1 ] • [v5 , v1 , v3 ] [v5 , v4 ] [v5 , v1 , v3 , v5 ]

Isabela Drămnesc UVT Graph Theory and Combinatorics – Lecture 10 25 / 25

You might also like