LecN4 R
LecN4 R
to Graph Theory
ä Graph theory is a fundamental tool in sparse matrix techniques.
• General definitions; Representations;
DEFINITION. A graph G is defined as a pair of sets G = (V, E)
• Graph Traversals; with E ⊂ V × V . So G represents a binary relation. The graph
• Topological sort; is undirected if the binary relation is symmetric. It is directed
otherwise. V is the vertex set and E is the edge set.
4-2 – graphBG
ä If (u, v) ∈ E, then v is adjacent to u. The edge (u, v) is ä A graph is nothing but a collection of vertices (indices from 1 to
incident to u and v. n), each with a set of its adjacent vertices [in effect a ’sparse matrix
ä If the graph is directed, then (u, v) is an outgoing edge from u without values’]
and incoming edge to v ä Therefore, can use any of the sparse matrix storage formats -
ä Adj(i) = {j|j adjacent to i} omit the real values arrays.
Adjacency matrix Assume V =
ä The degree of a vertex v is the number of edges incident to v.
{1, 2, · · · , n}. Then the adjacency 1 if (i, j) ∈ E
Can also define the indegree and outdegree. (Sometimes self-edge ai,j =
matrix of G = (V, E) is the n × n 0 Otherwise
i → i omitted)
matrix, with entries:
ä |S| is the cardinality of set S [so |Adj(i)| == deg( i) ]
ä A subgraph G0 = (V 0, E 0) of G is a graph with V 0 ⊂ V and
E 0 ⊂ E.
1 2 1
Null
1 2 Null
1
3
1 1
Null
1 4 3 4 Null
Example: 5 Null
2
1 1 1 6 Null
1 1
1 1 ä An array of linked lists. A linked list associated with vertex i,
1 1 contains all the vertices adjacent to vertex i.
4 3
ä General and concise for ’sparse graphs’ (the most practical situ-
ations).
ä Not too economical for use in sparse matrix methods
4-7 – graphBG 4-8 – graphBG
More terminology & notation - 2 Find cycles in this graph: A path in an indirected graph
1 5 1 5
ä A section graph is a clique if all the nodes in the subgraph are ä A path w0, . . . , wk is simple if the vertices w0, . . . , wk are
pairwise adjacent (→ dense block in matrix) distinct (except that we may have w0 = wk for cycles).
ä A path is a sequence of vertices w0, w1, . . . , wk such that ä An undirected graph is connected if there is path from every
(wi, wi+1) ∈ E for i = 0, . . . , k − 1. vertex to every other vertex.
ä The length of the path w0, w1, . . . , wk is k (# of edges in the ä A digraph with the same property is said to be strongly connected
path)
ä A cycle is a closed path, i.e., a path with wk = w0.
ä A graph is acyclic if it has no cycles.
4-9 – graphBG 4-10 – graphBG
ä The undirected form of a directed graph the undirected graph Root 1 5 Root 1 5
ä Tree traversal is a process of visiting all vertices in a tree. Typically ä Issue: systematic way of visiting all nodes of a general graph
traversal starts at root.
ä Two basic methods: Breadth First Search (wll’s see later) & ...
ä Want: systematic traversals of all nodes of tree – moving from a
ä Depth-First Search.
node to a child or parent
ä Preorder traversal: Visit parent before children [recursively] Algorithm List = DF S(G, v) (DFS from v)
In example: v1, v2, v9, v10, v3, v8, v6, v7, v5, v4 • Visit and Mark v;
• for all edges (v, w) do
ä Postorder traversal: Visit children before parent [recursively]
– if w is not marked then List = DF S(G, w)
In example : v10, v9, v2, v8, v7, v6, v3, v4, v5, v1
– Add v to top of list produced above
Depth First Search – undirected graph example Depth First Search – directed graph example
A A
B C
ä Assume adjacent nodes are B C
listed in alphabetical order.
- 3 DFS traversal from A ? D G
D G
E F
E F
The Problem: Given a Directed Acyclic Graph (DAG), order the Property exploited: An acyclic Digraph must have at least one
vertices from 1 to n such that, if (u, v) is an edge, then u appears vertex with indegree = 0.
before v in the ordering.
- 5 Prove this
ä Equivalently, label vertices from 1 to n so that in any (directed)
Algorithm:
path from a node labelled k, all vertices in the path have labels >k.
ä Many Applications ä First label these vertices as 1, 2, . . . , k;
ä Prerequisite requirements in a program ä Remove these vertices and all edges incident from them
ä Scheduling of tasks for any project ä Resulting graph is again acyclic ... ∃ nodes with indegree = 0.
label these nodes as k + 1, k + 2, . . . ,
ä Parallel algorithms;
ä Repeat..
ä ...
- 6 Explore implementation aspects.
4-23 – graphBG 4-24 – graphBG
Alternative methods: Topological sort from DFS Lst = DFS(G,v)
GRAPH MODELS FOR SPARSE MATRICES Graph Representations of Sparse Matrices. Recall:
• See Chap. 3 of text
Adjacency Graph G = (V, E) of an n × n matrix A :
• Sparse matrices and graphs.
V = {1, 2, ...., N } E = {(i, j)|aij 6= 0}
• Bipartite model, hypergraphs
4-28 – graph
- 10 Show the matrix pattern for the 1 5
Example: Adjacency graph of:
graph on the right and give an interpre- 2
3
tation of the path v4, v2, v3, v5, v1 on 4
? ?
the matrix 7 6 ? ?
ä A separator is a set Y of vertices such that the graph GX−Y is
? ? ? ?
disconnected. A= .
? ?
Example: Y = {v3, v4, v5} is a separator in the above figure ? ?
? ?
ä Two graphs are isomorphic is there is a mapping between the Bipartite graph representation
vertices of the two graphs that preserves adjacency.
- 11 Are the following 3 graphs isomorphic? If yes find the mappings ä Each row is represented by a vertex; Each column is represented
between them. by a vertex.
1
ä Relations only between rows and columns: Row i is connected
to column j if aij 6= 0
4 1 1 2
2 3
?
?
3 2
Example:
4 5
? ?
4
5
? ?
? ?
5 6 6 3 6
- 12 In which of the following cases is the underlying physical mesh - 16 What is the graph of Ak ?
the same as the graph of A (in the sense that edges are the same):
Theorem Let A be the adjacency matrix of a graph G = (V, E).
• Finite difference mesh [consider the simple case of 5-pt and 7-pt Then for k ≥ 0 and vertices u and v of G, the number of paths
FD problems - then 9-point meshes. ] of length k starting at u and ending at v is equal to (Ak )u,v .
• Finite element mesh with linear elements (e.g. triangles)?
• Finite element mesh with other types of elements? [to answer Proof: Proof is by induction.
this question you would have to know more about higher order ä Recall (definition): A matrix is reducible if it can be permuted
elements] into a block upper triangular matrix.
- 13 What is the graph of A + B (for two n × n matrices)? ä Note: A matrix is reducible iff its adjacency graph is not (strongly)
connected, i.e., iff it has more than one connected component.
- 14 What is the graph of AT ?
- 15 What is the graph of A.B?
4-33 – graph 4-34 – graph
C
●
B
A B C ä Definition : a graph is d regular if each vertex has the same
● ●
●
●
● ● ●
degree d.
ä No edges from
●
●
●
● Proposition: The spectral radius of a d regular graph is equal to d.
● ●
C to A or B. No
●
●
edges from B to A. Proof: The vector e of all ones is an eigenvector of A associated
● A
● ●
with the eigenvalue λ = d. In addition this eigenvalue is the largest
●
●
possible (consider the infinity norm of A). Therefore e is the Perron-
Frobenius vector u1.
Theorem: Perron-Frobenius An irreducible, nonnegative n × n
matrix A has a real, positive eigenvalue λ1 such that:
(i) λ1 is a simple eigenvalue of A;
(ii) λ1 admits a positive eigenvector u1 ; and
(iii)|λi| ≤ λ1 for all other eigenvalues λi where i > 1.
ä The spectral radius is equal to the eigenvalue λ1
To From Fr So. Ju. Sr. Grad lwd A few words about hypergraphs
Fr. .2 0 0 0 0 0
So. .6 .1 0 0 0 0 ä Hypergraphs are very general.. Ideas borrowed from VLSI work
Ju. 0 .7 .1 0 0 0
ä Main motivation: to better represent communication volumes
Sr. 0 0 .8 .1 0 0
when partitioning a graph. Standard models face many limitations
Grad 0 0 0 .75 1 0
lwd .2 .2 .1 .15 0 1 ä Hypergraphs can better express complex graph partitioning prob-
lems and provide better solutions.
- 17 What is P ? Assume initial population is x0 = [10, 16, 12, 12, 0, 0]
and do a follow the population for a few years. What is the probability ä Example: completely nonsymmetric patterns ...
that a student will graduate? What is the probability that s/he leaves ä .. Even rectangular matrices. Best illustration: Hypergraphs are
without a degree? ideal for text data
g = (z−2)*v
Back-Propagation
ä Corresponding compu- z = x+y
v = y+1 ä Often we want to compute the gradient of the function at the
tational graph:
root, once the nodes have been evaluated
y
x
ä The derivatives can be calculated by going backward (or down
ä Given values of x, y we want to (a) Evaluate the nodes and also the tree)
(b) derivatives of g w.r.t x, y at the nodes ä Here is a very simple example from Neural Networks
(a) is trivial - just follow the graph up - starting from the leaves (that 1
contain x and y) L = 2 (y − t)2 x t
y = σ(z) w L
(b): Use the chain rule – here shown for x only using previous setting z y
z = wx + b b
∂g ∂g da ∂g db
∂x
= ∂a dx
+ ∂b dx
ä Note that t (desired output) and x (input) are constant.
- 18 For the above example compute values at nodes and derivatives
when x = −1, y = 2.
4-43 – graph 4-44 – graph
∂f
Back-Propagation: General computational graphs ä Let δk = ∂vk
(called ‘errors’). Then
∂vj ∂vl ∂vm
δk = δj + δl + δm
∂vk ∂vk ∂vk
Example:
4-47 – graph