The Graph Abstract Data Type PowerPoint PPT Presentation

presentation player overlay
1 / 48
About This Presentation
Transcript and Presenter's Notes

Title: The Graph Abstract Data Type


1
The Graph Abstract Data Type
  • CS 5050
  • Chapter 6

2
Terminology
  • Vertex (vertices) position, holds an object
  • Edge also a position, holds an object
  • Directed, undirected ((a)symmetric)
  • Graph is a set of vertices and a bag of edges
    (can be duplicated)
  • Parallel and self-edges
  • Directed, undirected, mixed (both directed and
    undirected edges) convert to digraph
  • End vertices/endpoints (of an edge),
    origin/destination, adjacent vertices (endpoints
    of same edge)
  • Incident edge, incoming, outgoing, in (out)-degree

3
Terminology continued
  • Simple graphs have no parallel (multiple edges
    between same vertices) or self loop
  • Path sequence of alternating vertices and edges
    which match up
  • Cycle - path with same first and last vertex
  • Simple and directed paths and cycles
  • (Spanning contains all vertices) subgraph
  • Connected graph, connected component
  • Forest (acyclic), free (no root) trees
    (connected forest), spanning tree

4
Graph Facts
  • Assume the graph G has n vertices (V) and m edges
    (E)
  • The sum over V of degrees is 2m
  • If G is directed, the sum over V of in-degrees
    the sum over V of out-degrees m
  • If G is simple and undirected, m lt n(n-1)/2
  • (worst case is every pair)
  • If G is simple and directed, m lt n(n-1)
  • So m is O( V2 )
  • If G is connected, m gt n-1, if a tree m n-1
  • If G is a forest, m lt n-1

5
Adjacency List -partitioned incidence container
(in/out)
6
Adjacency Matrix
7
Traversals
  • Visit each node e.g., web crawler
  • Have to restart traversal in each connected
    component, but this allows us to identify
    components
  • Reachability in a digraph is an important issue
    the transitive closure graph
  • Book permits counter-direction motion in general
    traversals

8
Looking for a spanning Tree
  • Many times algorithms depend on some ordering of
    the nodes of a graph
  • Labeling the edges is some way is helpful in
    organizing thinking about a graph

9
Depth-First Search (undirected graph)
  • Simple recursive backtracking algorithm calls
    recursive function at starting point
  • For each incident edge to a vertex
  • If opposite (other) vertex is unvisited
  • Label edge as discovery
  • Recur on the opposite vertex
  • Else label edge as back
  • Discovery edges form a component spanning tree,
    back edges go to an ancestor
  • with m edges, O(m) using an adjacency list, but
    not using an adjacency matrix

10
Depth-First Traversal
11
Biconnectivity
SEA
PVD
ORD
FCO
SNA
MIA
12
Outline and Reading
  • Definitions (6.3.2)
  • Separation vertices and edges
  • Biconnected graph
  • Biconnected components
  • Equivalence classes
  • Linked edges and link components
  • Algorithms (6.3.2)
  • Auxiliary graph
  • Proxy graph

13
Separation Edges and Vertices
  • Definitions
  • Let G be a connected graph
  • A separation edge of G is an edge whose removal
    disconnects G
  • A separation vertex of G is a vertex whose
    removal disconnects G
  • Applications
  • Separation edges and vertices represent single
    points of failure in a network and are critical
    to the operation of the network
  • Example
  • DFW, LGA and LAX are separation vertices
  • (DFW,LAX) is a separation edge

ORD
PVD
SFO
LGA
HNL
LAX
DFW
MIA
14
Biconnected Graph
  • Equivalent definitions of a biconnected graph G
  • Graph G has no separation edges and no separation
    vertices
  • For any two vertices u and v of G, there are two
    disjoint simple paths between u and v (i.e., two
    simple paths between u and v that share no other
    vertices or edges)
  • For any two vertices u and v of G, there is a
    simple cycle containing u and v
  • Example

PVD
ORD
SFO
LGA
HNL
LAX
DFW
MIA
15
Biconnected Components
  • Biconnected component of a graph G
  • A maximal biconnected subgraph of G, or
  • A subgraph consisting of a separation edge of G
    and its end vertices
  • Interaction of biconnected components
  • An edge belongs to exactly one biconnected
    component
  • A nonseparation vertex belongs to exactly one
    biconnected component
  • A separation vertex belongs to two or more
    biconnected components
  • Example of a graph with four biconnected
    components

ORD
PVD
SFO
LGA
RDU
HNL
LAX
DFW
MIA
16
Equivalence Classes
  • Given a set S, a relation R on S is a set of
    ordered pairs of elements of S, i.e., R is a
    subset of S?S
  • An equivalence relation R on S satisfies the
    following properties
  • Reflexive (x,x) ? R
  • Symmetric (x,y) ? R ? (y,x) ? R
  • Transitive (x,y) ? R ? (y,z) ? R ? (x,z) ? R
  • An equivalence relation R on S induces a
    partition of the elements of S into equivalence
    classes
  • Example (connectivity relation among the vertices
    of a graph)
  • Let V be the set of vertices of a graph G
  • Define the relation C (v,w) ? V?V such that G
    has a path from v to w
  • Relation C is an equivalence relation
  • The equivalence classes of relation C are the
    vertices in each connected component of graph G

17
Link Relation
  • Edges e and f of connected graph G are linked if
  • e f, or
  • G has a simple cycle containing e and f
  • Theorem
  • The link relation on the edges of a graph is an
    equivalence relation
  • Proof Sketch
  • The reflexive and symmetric properties follow
    from the definition
  • For the transitive property, consider two simple
    cycles sharing an edge

Equivalence classes of linked edges a b, c,
d, e, f g, i, j
i
g
e
b
a
d
j
f
c
18
Link Components
  • The link components of a connected graph G are
    the equivalence classes of edges with respect to
    the link relation
  • A biconnected component of G is the subgraph of G
    induced by an equivalence class of linked edges
  • A separation edge is a single-element equivalence
    class of linked edges
  • A separation vertex has incident edges in at
    least two distinct equivalence classes of linked
    edge

ORD
PVD
SFO
LGA
RDU
HNL
LAX
DFW
MIA
19
Auxiliary Graph
h
g
  • Auxiliary graph B for a connected graph G
  • Associated with a DFS traversal of G
  • The vertices of B are the edges of G
  • For each back edge e of G, B has edges (e,f1),
    (e,f2) , , (e,fk),where f1, f2, , fk are the
    discovery edges of G that form a simple cycle
    with e
  • Its connected components correspond to the the
    link components of G

i
e
b
i
j
d
c
f
a
DFS on graph G
g
e
i
h
b
f
j
d
c
a
Auxiliary graph B
20
Auxiliary Graph (cont.)
  • In the worst case, the number of edges of the
    auxiliary graph is proportional to nm

Auxiliary graph B
DFS on graph G
21
Proxy Graph
h
g
Algorithm proxyGraph(G) Input connected graph
G Output proxy graph F for G F ? empty
graph DFS(G, s) s is any vertex of G for all
discovery edges dEdge of G F.insertVertex(dEdge)
setLabel(dEdge, UNLINKED) for all vertices v of
G in DFS visit order for all back edges bEdge
(u,v) F.insertVertex(bEdge) repeat dEdge
? discovery edge with dest. u F.insertEdge(bEdg
e,dEdge,?) if getLabel(dEdge)
UNLINKED setLabel(dEdge, LINKED) u ?
origin of dEdge else u ? v ends the
loop until u v return F
i
e
b
i
j
d
c
f
a
DFS on graph G
g
e
i
h
b
f
j
d
c
a
Proxy graph F
22
Proxy Graph (cont.)
h
g
  • Proxy graph F for a connected graph G
  • Spanning forest of the auxiliary graph B
  • Has m vertices and O(m) edges
  • Can be constructed in O(n m) time
  • Its connected components (trees) correspond to
    the the link components of G
  • Given a graph G with n vertices and m edges, we
    can compute the following in O(n m) time
  • The biconnected components of G
  • The separation vertices of G
  • The separation edges of G

i
e
b
i
j
d
c
f
a
DFS on graph G
g
e
i
h
b
f
j
d
c
a
Proxy graph F
23
Breadth-First Search
  • By levels, typically using queues

24
BFS Facts
  • There are discovery and cross edges
  • (why no back edges?)
  • Once marked, dont follow again.
  • Discovery edges form spanning tree
  • Tree edges are paths, minimal in length
  • Cross edges differ by at most one in level
  • Try writing the code to do a BFS

25
Thm 6.19 Algorithms based on BFS
  • Test for connectivity
  • compute spanning forest
  • compute connected components
  • find shortest path between two points (in number
    of links)
  • compute a cycle in graph, or report none
  • (have cross edges)
  • Good for shortest path information, while DFS
    better for complex connectivity questions

26
Digraphs
  • A digraph is a graph whose edges are all directed
  • Short for directed graph
  • Applications
  • one-way streets
  • flights
  • task scheduling

27
Digraph Properties
  • A graph G(V,E) such that
  • Each edge goes in one direction
  • Edge (a,b) goes from a to b, but not b to a.
  • If G is simple, m lt n(n-1).
  • If we keep in-edges and out-edges in separate
    adjacency lists, we can perform listing of
    in-edges and out-edges in time proportional to
    their size.

28
Digraph Application
  • Scheduling edge (a,b) means task a must be
    completed before b can be started

ics23
ics22
ics21
ics53
ics51
ics52
ics161
ics131
ics141
ics121
ics171
The good life
ics151
29
Digraph Facts
  • Directed DFS gives directed paths from root to
    each reachable vertex
  • Used for O(n(nm)) algorithm dfs is O(nm),
    these algorithms use n dfs searches
  • Find all induced subgraphs (from each vertex, v,
    find subgraph reachable from v)
  • Test for strong connectivity
  • Compute the transitive closure
  • Directed BFS has discovery, back, cross edges

30
Directed DFS
  • We can specialize the traversal algorithms (DFS
    and BFS) to digraphs by traversing edges only
    along their direction
  • In the directed DFS algorithm, we have four types
    of edges
  • discovery edges
  • back edges
  • forward edges
  • cross edges
  • A directed DFS starting a a vertex s determines
    the vertices reachable from s

E
D
C
B
A
31
Reachability
  • DFS tree rooted at v vertices reachable from v
    via directed paths

E
D
E
D
C
A
C
F
E
D
A
B
C
F
A
B
32
Strong Connectivity
  • Each vertex can reach all other vertices

33
Strong Connectivity Algorithm
  • Pick a vertex v in G.
  • Perform a DFS from v in G.
  • If theres a w not visited, print no.
  • Let G be G with edges reversed.
  • Perform a DFS from v in G.
  • If theres a w not visited, print no.
  • Else, print yes.
  • Running time O(nm).

a
G
g
c
d
e
b
f
a
g
G
c
d
e
b
f
34
Strongly Connected Components
  • Maximal subgraphs such that each vertex can reach
    all other vertices in the subgraph
  • Can also be done in O(nm) time using DFS, but is
    more complicated (similar to biconnectivity).

a , c , g
f , d , e , b
35
Transitive Closure
D
E
  • Given a digraph G, the transitive closure of G is
    the digraph G such that
  • G has the same vertices as G
  • if G has a directed path from u to v (u ? v), G
    has a directed edge from u to v
  • The transitive closure provides reachability
    information about a digraph

B
G
C
A
D
E
B
C
A
G
36
Computing the Transitive Closure
  • We can perform DFS starting at each vertex
  • O(n(nm))
  • Alternatively ... Use dynamic programming The
    Floyd-Warshall Algorithm

37
Floyd-Warshall Transitive Closure
  • Idea 1 Number the vertices 1, 2, , n.
  • Idea 2 Consider paths that use only vertices
    numbered 1, 2, , k, as intermediate vertices

Uses only vertices numbered 1,,k (add this edge
if its not already in)
i
j
Uses only vertices numbered 1,,k-1
Uses only vertices numbered 1,,k-1
k
38
Floyd-Warshalls Algorithm
  • Floyd-Warshalls algorithm numbers the vertices
    of G as v1 , , vn and computes a series of
    digraphs G0, , Gn
  • G0G
  • Gk has directed edge (vi, vj) if G has directed
    path from vi to vj with intermediate vertices in
    the set v1 , , vk
  • We have that Gn G
  • In phase k, digraph Gk is computed from Gk - 1
  • Running time O(n3), assuming areAdjacent is O(1)
    (e.g., adjacency matrix)

Algorithm FloydWarshall(G) Input digraph G
(vertices numbered) Output transitive closure G
of G G0 ? G for k ? 1 to n do Gk ? Gk - 1
for i ? 1 to n (i ? k) do for j ? 1 to n (j ?
i, k) do if Gk - 1.areAdjacent(vi vk) ?
Gk - 1.areAdjacent(vk vj) if
?Gk.areAdjacent(vi vj) Gk.insertDirectedEdge
(vi vj) return Gn
39
Floyd-Warshall Example
BOS
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
40
(No Transcript)
41
Floyd-Warshall, Conclusion
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
42
DAGs and Topological Ordering
D
E
  • A directed acyclic graph (DAG) is a digraph that
    has no directed cycles
  • A topological ordering of a digraph is a
    numbering
  • v1 , , vn
  • of the vertices such that for every edge (vi ,
    vj), we have i lt j
  • Example in a task scheduling digraph, a
    topological ordering a task sequence that
    satisfies the precedence constraints
  • Theorem
  • A digraph admits a topological ordering if and
    only if it is a DAG

B
C
A
DAG G
v4
v5
D
E
v2
B
v3
C
v1
Topological ordering of G
A
43
Topological Sorting
  • Number vertices, so that (u,v) in E implies u lt v

1
A typical student day
wake up
3
2
eat
study computer sci.
5
4
nap
more c.s.
7
play
8
write c.s. program
6
9
work out
make cookies for professors

10
11
sleep
dream about graphs
44
Algorithm for Topological Sorting
  • Note This algorithm is different than the one in
    Goodrich-Tamassia
  • Running time O(n m). How?

Method TopologicalSort(G) H ? G //
Temporary copy of G n ? G.numVertices()
while H is not empty do Let v be a vertex with
no outgoing edges Label v ? n n ? n - 1 Remove
v from H
45
Topological Sorting Algorithm using DFS
  • Simulate the algorithm by using depth-first
    search
  • O(nm) time.

Algorithm topologicalDFS(G, v) Input graph G and
a start vertex v of G Output labeling of the
vertices of G in the connected component of v
setLabel(v, VISITED) for all edgeE ?
G.incidentEdges(v) if getLabel(edgeE)
UNEXPLORED w ? opposite(v,edgeE) if
getLabel(w) UNEXPLORED setLabel(edgeE,
DISCOVERY) topologicalDFS(G, w) else e
is a forward or cross edge Label v with
topological number n n ? n - 1
Algorithm topologicalDFS(G) Input dag G Output
topological ordering of G n ?
G.numVertices() for all u ? G.vertices()
setLabel(u, UNEXPLORED) for all e ? G.edges()
setLabel(e, UNEXPLORED) for all v ?
G.vertices() if getLabel(v)
UNEXPLORED topologicalDFS(G, v)
46
Topological Sorting Example
9
47
Topological Sorting Example
2
3
4
6
5
7
8
9
48
Topological Sorting Example
2
1
3
4
6
5
7
8
9
Write a Comment
User Comments (0)
About PowerShow.com