Professional Documents
Culture Documents
Adc 7
Adc 7
Course 7
Overview
Graphs – Introduction
Representing Graphs
Practical Examples of Using Graphs
Search Algorithms
BFS
DFS
Topological Sorting
Graphs – Introduction
Graphs are very important data structures as the model a
lot of real-life objects
There are a lot of problems that must be solved for
graphs
Some of them are very difficult (NP-complete)
Others are in P
- Useful for:
- Generating graphs
- Visualizing graphs
- Modeling graphs
Representing Graphs
As input data:
Pairs of vertices representing the edges: (Src, Dest)
Specialized data formats for representing graphs
RDF
GraphML
dot
Adj[u] ; u V E F
I F
G B C
A J
B
H A
G K I A J K
C J K
H
L
K L
D
E L
F
Adjacency Matrix
A[i, j] =
A B C D E F G H I J K L
1 if (i, j) E A 1 1
1
B
0 if (i, j) E C
D
1 1
E 1
I
F
G 1 1
A J H 1
B
I 1 1 1
J 1
G K K 1
C L
H
L
D
E
F
Incidence Matrix
B[u, e]
u V
eE
= 1 if edge e leaves vertex u
= -1 if edge e enters vertex u
= 0 otherwise
Adjacency Matrix vs Adjacency Lists
Which one is better ?
Answer: It depends
Graphs may be:
Sparse: m = O(n)
Dense: m = (n2)
Adjacency matrix:
Space required: (n2)
Time for going through all edges: (n2)
Time for finding if an edge exists: (1)
Adjacency lists:
Space required: (n+m)
Time for going through all edges: (n+m)
Time for finding if an edge exists: (max(|Adj(u)|))
|V| = n, |E| = m
Matrix is better for dense graphs, lists are better for sparse graphs
Practical Examples of Using Graphs
Maps (roads, etc.), networks (computer, electric, etc.),
Web, flow networks (traffic – cars or computer
networks, pipes, etc.), relations between
processes/activities…
Simple examples:
The shortest path on Google Maps.
The people that are most central (or most important) in a
social network
Google PageRank
The Web + PageRank
http://en.wikipedia.org/wiki/PageRank
Computer Networks
http://ist.marshall.edu/ist362/pics/OSPF.gif
Social Web
Semantic Web
Source: http://www.semanticfocus.com/media/insets/rdf-graph.png
Linked Open Data on the Web
(http://richard.cyganiak.de/2007/10/lod/)
Graph Search Algorithms
Graph search (traversal)
A methodology to go through all the nodes in a graph
The result is a list of nodes in the order that they are
visited
This list should be useful to us!
Should contain important information about the graph
Q = B, C Q = C, H Q = H, D, E Q=F
Q = D, E Q=E Q=Ø
d(C) = 2 d(H) = 2 d(D)=d(E)=3 d(F)=4
I I I I I
I I
A A A A A
A A J J J J J
J J B B B B B
B B
G /G G G G
G G K K K
K K K K
C C C C C C C
H H H H H H H
L L D L L D L L L
D D D D D
E E E E E F E E
F F
F F F F
color[u] =
BLACK if uS
GREY if uQ
WHITE if uV \ Q \ S
p[u] =
!= NULL if uQ U S
NULL if uV \ Q \ S
d[u] =
!= INF if uQ U S
INF if uV \ Q \ S
Using all the above properties, we can prove that d[v] = δ(s,v)
vV
Thus BFS is correct!
Depth First Search (DFS)
There is no source vertex
All the vertices of the graph are traversed
This traversal does not compute the shortest distance
between vertices, but has a lot of useful applications
Finishing a vertex u:
When the search leaves the vertex
All the nodes that could have been discovered from that
vertex are either GREY or BLACK
There is no WHITE vertex in R(u)
Changes color from GREY to BLACK
Data Structures
We need a stack (LIFO) in order to implement the
traversal in order to discover all the nodes in order of
how they are reached
The predecessors are lower in the stack
For each node u, we use:
p[u]
color[u]
d[u]
f[u]
A 1/16
A J 18/
J B 2/5
B
G K K
G 6/15
C C 7/14
H
L H 3/4
D L
D 8/9
E
F E 10/13
F 11/12
DFS – Example (2)
Some of the steps have beenI omitted
A J
B
G K
C
H
D L
E
I I I I I I
A A A A A A
J J J J J J
B B B B B B
G G G G G G
K K K K K K
C C C C C C
H H H H H H
L L L L L L
D D D D D D
E E E E E E
F F F F F F
DFS Tree – Example
In fact, the edges have the opposite sense than the one
represented in the figure
I
A J
B
G K
C
H
L
D
F
DFS – Properties
The DFS forest can be formally defined as:
If G is undirected
G is a connected graph <=> Arb(G) has a single tree
For a given graph, running DFS may build different DFS forests
(and thus different DF traversals)
Depending on the order of choosing the roots of the trees
Depending on how the elements in Adj(u) are chosen
Parenthesis Theorem
u, v V, there are three correct alternatives to arrange
the discovery and finish times of the two nodes:
d[u] d[v] f[v] f[u] (u … (v … v) … u)
v is a descendant of u in the DFS tree
d[u] f[u] d[v] f[v] (u … u) (v … v)
there is no direct descendent relation between u and v
d[v] f[v] d[u] f[u] (v … v) (u … u)
there is no direct descendent relation between u and v
u and v may be in different trees or on different paths in the same
tree
(u = discovery of u
u) = finishing of u
White Path Theorem
At time d[u], any node v that is:
WHITE and
Reachable from u (in R(u))
There exists a path u..v that consists of only WHITE vertices
(except u that is GRAY)
Alternative:
v is a descendent of u in a DFS tree <=> there exists a path
that consists of only WHITE vertices (except u that is GRAY)
Edge Classification
(u, v)E is in one of the following classes:
Tree edge
Any edge that is part of a DFS tree
Back edge
Any edge from a node to one of its ancestors in the DFS tree
Forward edge
Any edge from a node to one of its descendants that are not its children
Cross edge
Any other edge that cannot be classified in one of the above classes
Back edge
Any edge from a node to one of its ancestors in the DFS tree
(u, v) => u – GREY ; v - GREY
Forward edge
Any edge from a node to one of its descendants that are not its children
(u, v) => u – GREY ; v - BLACK
Cross edge
Any other edge that cannot be classified in one of the above classes
(u, v) => u – GREY ; v - BLACK
DFS:
Topological Sorting
Strongly Connected Components
Articulation Points
Bridges
Biconnected Components
Topological Sorting
Given a DAG (Directed Acyclic Graph)
Used in real applications:
Activity diagrams:
Nodes: activities
Edges: dependencies between activities
Bayesian networks
Combinatorial logic
Compilers
Is v:
GRAY? NO, because (u, v) would be a back edge in a DAG.
Impossible!
WHITE? Then d[u] < d[v] < f[v] < f[u] (Parenthesis theorem, Tree
edge)
BLACK? Then d[v] < f[v] < d[u] < f[u] (Cross edge)
or d[u] < d[v] < f[v] < f[u] (Forward edge)
Conclusions
We have seen that graphs are very important in modeling
structures from the real world
Lots of applications