Graph Theory Seminar
-
Upload
puneet-patil -
Category
Documents
-
view
215 -
download
0
Transcript of Graph Theory Seminar
-
7/22/2019 Graph Theory Seminar
1/50
GRAPH THEORYBy: Puneet Patil
-
7/22/2019 Graph Theory Seminar
2/50
A model
Many structures in real world can be represented on
paper by means of a diagram consisting of a set of points
together with lines joining some or all pairs of these points.
For example,
Avertex: an objectAn e ge : a relation between two objects
-
7/22/2019 Graph Theory Seminar
3/50
What is a graph?
A graph G consists of a set of vertices and a set of edges.
G = {V,E}
In this example, V = {A,B,C,D,E,F}, E = {AB, BC, BD, CD,
DF, DE,EF}.Any graph can be drawn on paper in many waysthe
important thing is which vertices are connected (adjacent)
to each other.
-
7/22/2019 Graph Theory Seminar
4/50
Why study graph theory?
I Useful set of techniques for solving real-world problems
particularly for different kinds of optimization.
I Graph theory is useful for analyzing things that are
connected to other things, which applies almost
everywhere.
I Some difficult problems become easy when represented
using a graph.
I There are lots of unsolved questions in graph theory:
solve one and become rich and famous1.
-
7/22/2019 Graph Theory Seminar
5/50
Graphs Networks
Graph
(Network)
Vertexes
(Nodes)
Edges
(Arcs)Flow
CommunicationsTelephonesexchanges,
computers, satellites
Cables, fiber optics,microwave relays Voice, video, packets
CircuitsGates, registers,processors
Wires Current
Mechanical JointsRods, beams,springs
Heat, energy
HydraulicReservoirs, pumping
stations, lakes Pipelines Fluid, oil
Financial Stocks, currency Transactions Money
TransportationAirports, rail yards,street intersections
Highways, rail beds,airway routes
Freight, vehicles,passengers
-
7/22/2019 Graph Theory Seminar
6/50
Adjacent, Loop, Multiple edges Two vertices are adjacent and are neighbors if they are
the endpoints of an edge
Example:
Aand Bare adjacent
Aand Dare not adjacent
Loop: An edge whose endpoints are equal.
Multiple edges : Edges have the same pair of endpoints.
Simple graph : A graph has no loops
or multiple edges.
-
7/22/2019 Graph Theory Seminar
7/50
Directed Graph
An edge e Eof a directed graph is
represented as an ordered pair (u,v),where u,
v V. Here uis the initial vertex and vis the
terminal vertex. Also assume here that u v 2
4
31
V= { 1, 2, 3, 4}, | V | = 4
E= {(1,2), (2,3), (2,4), (4,1), (4,2)}, | E|=5
-
7/22/2019 Graph Theory Seminar
8/50
Undirected Graph
2
4
31
V= { 1, 2, 3, 4}, | V | = 4
E= {(1,2), (2,3), (2,4), (4,1)}, | E|=4
An edge e Eof an undirected graph is
represented as an unordered pair
(u,v)=(v,u),where u, v V. Also assume
that u v
-
7/22/2019 Graph Theory Seminar
9/50
Degree of a Vertex
Degreeof a vertex in an undirected graph is the numberof edges incident on it. In a directed graph, theout
degreeof a vertex is the number of edges leaving it andthe in degreeis the number of edges entering it
2
4
31
2
4
31
The degreeof vertex 2
is 3
The indegreeof vertex 2
is 2 and the in degreeof
vertex 4 is 1
-
7/22/2019 Graph Theory Seminar
10/50
Weighted Graph
A weighted graph is a graph for which each edge has an
associated weight, usually given by a weight function w: E
R
2
4
31
2
4
31
1.2
2.1
0.2
0.5 4
8
6
2
9
-
7/22/2019 Graph Theory Seminar
11/50
Walks and Paths3
2
3
4
1
1
V5V4
V3V2
V1V6
4
1
A walkis an sequence of nodes (v1, v2,..., vL) such that
{(v1, v2), (v1, v2),..., (v1, v2)} E, e.g. (V2, V3,V6, V5,V3)
A simple pathis a walk with no repeated nodes, e.g. (V1,
V4,V5, V2,V3) A cycleis an walk (v1, v2,..., vL) where v1=vLwith no other
nodes repeated and L>3, e.g. (V1, V2,V5, V4,V1)
A graph is called cyclicif it contains a cycle; otherwise it iscalled acyclic
-
7/22/2019 Graph Theory Seminar
12/50
Complete Graphs
A
D
C
B
4 nodes and (4*3)/2 edges
Vnodes and V*(V-1)/2
edges
C
A
B
3 nodes and 3*2 edges
Vnodes and V*(V-1)
edges
A complete graph is an undirected/directed graph in which
every pair of vertices is adjacent. If (u, v ) is an edge in agraph G, we say that vertex v is adjacent to vertex u.
-
7/22/2019 Graph Theory Seminar
13/50
Connected GraphsA
D E F
B C
A B
C D
An undirected graph is connectedif
you can get from any node to any
other by following a sequence ofedges OR any two nodes are
connected by a path
-
7/22/2019 Graph Theory Seminar
14/50
Bipartite Graph
A bipartite graph
is an undirected graph
G = (V,E) in which V can be
partitioned into 2 sets V1 and V2
such that ( u,v) E implies
either
u V1 and v V2
OR
v V1 and u V2.
u1
u2
u3
u4
v1
v2
v3
V1 V2
-
7/22/2019 Graph Theory Seminar
15/50
Trees
A
B
D
F
C
E
Let G = (V, E) be an undirected graph.
The following statements are equivalent,
1. Gis a tree
2. Any two vertices in Gare connected
by unique simple path3. Gis connected, but if any edge is
removed from E, the resulting graph is
disconnected
4. G is connected, and | E| = | V| -15. G is acyclic, and | E| = | V| -1
6. G is acyclic, but if any edge is added
to E, the resulting graph contains a
cycle
-
7/22/2019 Graph Theory Seminar
16/50
Planar graph
If graph can be drawn in the plane without any of its
edges crossing each other, then it is called Planar graph.
Otherwise its called as Non planar graph.
Non planar Planar
-
7/22/2019 Graph Theory Seminar
17/50
Adjacency matrix
LetG= (V, E), |V| = nand|E|=m
The dj cency m trixofGwrittenA(G), is the n-by-nmatrix in which entry ai,jis the number of edges inG
with endpoints {vi, vj}.
a
b
c
d
e
w
x
y z
w x y z
0 1 1 0
1 0 2 0
1 2 0 1
0 0 1 0
w
x
y
z
-
7/22/2019 Graph Theory Seminar
18/50
Incidence Matrix
LetG= (V, E), |V| = nand|E|=m
The incidence m trix M(G) is the n-by-mmatrix inwhich entrymi,jis1 ifviis an endpoint ofeiand
otherwise is 0.
a
b
c
d
e
w
x
y z
a b c d e
1 1 0 0 0
1 0 1 1 0
0 1 1 1 1
0 0 0 0 1
w
x
y
z
-
7/22/2019 Graph Theory Seminar
19/50
Isomorphism
An isomorphism from a simple graph Gto a simplegraphHis a bijection f:V(G)V(H) such thatuvE(G) if
and only if f(u)f(v) E(H)
We say G s isomorphic to H, writtenG H
HG
w
x z
y c d
ba
f1: w x y z
c b d a
f2: w x y z
a d b c
20
-
7/22/2019 Graph Theory Seminar
20/50
20
Example:H1, H2, andH3are subgraphs of G
c
d
a b
de
a b
c
deH1
G
H3H2
a b
cde
Subgraph
-
7/22/2019 Graph Theory Seminar
21/50
Clique- is a complete sub graph.
Clique number (G) - is cardinality of its largest clique.
Clique cover number (G) - is the minimum number
of cliques in G needed to cover the vertex set of G. Stable set (G) - is a subset of vertices with the property
that no two vertices in the stable set are adjacent.
Chromatic number (G) - is the smallest number of
colors needed to color the vertices of so that no twoadjacent vertices share the same color.
(G) (G)
(G) (G)
-
7/22/2019 Graph Theory Seminar
22/50
Branch & Bound algorithm
To apply BnB, one must be able to partition the solution
space into subsets. Often the subsets are arranged in a
tree structure. Leaves in the tree are solutions. Internal
nodes represent the set of solutions below them in the
tree (the subtree).
-
7/22/2019 Graph Theory Seminar
23/50
How Branch-and-Bound Works Some nodes in the tree are active.These are the nodes
waiting to be explored. Branching refers to exploring the sub tree of an active
node.
Bounding refers to estimating a bound on the solutions in
the sub tree rooted at the active node.
-
7/22/2019 Graph Theory Seminar
24/50
The Branch-and-Bound Algorithmbegin
activeset :=f0g;
bestval:=NULL;
currentbest:=NULL;
while activeset is not empty do
choose a branching node, node k 2 activeset;
remove node k from activeset;
generate the children of node k, child i, i=1,. . . ,nk,
and corresponding optimistic bounds obi;
for i=1 to nk do
if obi worse than bestval then kill child i;
else if child is a complete solution thenbestval:=obi, currentbest:=child i;
else add child i to activeset
end for
end while
end
-
7/22/2019 Graph Theory Seminar
25/50
Example
-
7/22/2019 Graph Theory Seminar
26/50
Dynamic Programming
Dynamic Programming is a general algorithm designtechnique for solving problems defined by or formulated asrecurrences with overlapping sub instances
Programming here means planning
Main idea:- set up a recurrence relating a solution to a larger instance
to solutions of some smaller instances- solve smaller instances once
- record solutions in a table- extract solution to the initial instance from that table
-
7/22/2019 Graph Theory Seminar
27/50
Example
-
7/22/2019 Graph Theory Seminar
28/50
Ingredients:
Instances:The possible inputs to the problem.
Solutions for Instance:Each instance has an exponentially large
set of valid solutions.
Cost of Solution:Each solution has an easy-to-compute cost or
value.
SpecificationPreconditions:The input is one instance.
Postconditions:A valid solution with optimal cost. (minimum
or maximum)
Optimization Problems
-
7/22/2019 Graph Theory Seminar
29/50
Greedy Solutions to Optimization
Problems
Surprisingly, many important and practical
optimization problems can be solved this way.
Every two-year-old knows the greedy algorithm.
In order to get what you want,
just start grabbing what looks best.
-
7/22/2019 Graph Theory Seminar
30/50
Example 1: Making Change
Problem:Find the minimum # of quarters, dimes,nickels, and pennies that total to a given amount.
-
7/22/2019 Graph Theory Seminar
31/50
The Greedy Choice
Commit to the object that looks the ``best''
Must prove that this locally greedy choice
does not have negative global consequences.
-
7/22/2019 Graph Theory Seminar
32/50
Instance:A drawer full of coins and an amount of change to return
25 25 25 25 25 25 25 25 25 25
10 10 10 10 10 10 10 10 10 10
5 5 5 5 5
5
5
5
5
5
1 1 1 1 1 1 1 1 1 1
Amount = 92
Solutions for Instance:
A subset of the coins in the drawer that total the amount
Making Change Example
-
7/22/2019 Graph Theory Seminar
33/50
Instance:A drawer full of coins and an amount of change to return
Solutions for Instance:A subset of the coins that total the amount.
25 25 25 25 25 25 25 25 25 25
10 10 10 10 10 10 10 10 10 10
5 5 5 5 5 5 5 5 5 5
1 1
1
1
1
1
1
1
1
1
Amount = 92
Cost of Solution:The number of coins in the solution = 14
Making Change Example
Goal:Find an optimal valid solution.
-
7/22/2019 Graph Theory Seminar
34/50
25 25 25 25 25 25 25 25 25 25
10 10 10 10 10 10 10 10 10 10
5 5 5 5 5 5 5 5 5 5
1 1 1 1 1 1 1 1 1 1
Amount = 92
Making Change Example
Greedy Choice:
Does this lead to an optimal # of coins?
Start by grabbing quarters until exceeds amount,
then dimes, then nickels, then pennies.
Cost of Solution:7
Instance:A drawer full of coins and an amount of change to return
-
7/22/2019 Graph Theory Seminar
35/50
HardMaking Change Example
Greedy Choice:Start by grabbing a 4-cent coin.
Problem:Find the minimum # of
4, 3, and 1 cent coins to make up 6 cents.
Consequences:
4+1+1 = 6 mistake
3+3=6 better
Greedy Algorithm does not work!
-
7/22/2019 Graph Theory Seminar
36/50
Shortest Path
Generalize distance to weighted setting
Digraph G= (V,E) with weight function W: ER(assigning real values to edges)
Weight of pathp= v1v2 vkis
Shortest path = a path of the minimum weight
Applications
static/dynamic network routing robot motion planning
map/route generation in traffic
1
1
1
( ) ( , )k
i i
i
w p w v v
-
7/22/2019 Graph Theory Seminar
37/50
Shortest-Path Problems Shortest-Path problems
Single-source (single-destination). Find a shortest path from a
given source (vertex s) to each of the vertices.
Single-pair. Given two vertices, find a shortest path between them.
Solution to single-source problem solves this problem efficiently,
too.
All-pairs. Find shortest-paths for every pair of vertices. Dynamic
programming algorithm.
-
7/22/2019 Graph Theory Seminar
38/50
Negative Weights and Cycles?
Negative edges are OK, as long as there are no negative
weight cycles (otherwise paths with arbitrary small
lengths would be possible)
Shortest-paths can have no cycles (otherwise we could
improve them by removing cycles) Any shortest-path in graph Gcan be no longer than n 1 edges,
where nis the number of vertices
-
7/22/2019 Graph Theory Seminar
39/50
Relaxation For each vertex v in the graph, we maintain v.d(), the
estimate of the shortest path from s, initialized to at the
start
Relaxing an edge (u,v) means testing whether we can
improve the shortest path to vfound so far by going
through u
5
u v
vu
2
2
9
5 7
Relax(u,v)
5
u v
vu
2
2
6
5 6
Relax(u,v)
Relax (u,v,G)
if v.d() > u.d()+G.w(u,v) then
v.setd(u.d()+G.w(u,v))
v.setparent(u)
-
7/22/2019 Graph Theory Seminar
40/50
Dijkstra's Algorithm
Non-negative edge weights
Greedy, similar to Prim's algorithm for MST
Like breadth-first search (if all weights = 1, one can simplyuse BFS)
Use Q, a priority queue ADT keyed by v.d() (BFS usedFIFO queue, here we use a PQ, which is re-organizedwhenever some ddecreases)
Basic idea
maintain a set Sof solved vertices at each step select "closest" vertex u, add it to S, and relax all
edges from u
-
7/22/2019 Graph Theory Seminar
41/50
Dijkstras Pseudo Code
Input: Graph G, start vertex s
relaxing
edges
Dijkstra(G,s)
01 foreachvertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)
05 S // Set S is used to explain the algorithm
06 Q.init(G.V()) // Q is apriority queueADT
07whilenotQ.isEmpty()08 u Q.extractMin()
09 S S {u}
10 foreachv u.adjacent() do
11 Relax(u, v, G)
12 Q.modifyKey(v)
-
7/22/2019 Graph Theory Seminar
42/50
Dijkstras Example
0s
u v
yx
10
5
1
2 39
4 67
2
10
5
0s
u v
yx
10
5
1
2 39
4 67
2
Dijkstra(G,s)
01 foreachvertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)05 S
06 Q.init(G.V())
07whilenotQ.isEmpty()08 u Q.extractMin()
09 S S {u}
10 foreachv u.adjacent() do11 Relax(u, v, G)
12 Q.modifyKey(v)
-
7/22/2019 Graph Theory Seminar
43/50
Dijkstras Example (2)
u v
8 14
5 7
0s
yx
10
5
1
2 39
4 67
2
8 13
5 7
0s
u v
yx
10
5
1
2 39
4 67
2
Dijkstra(G,s)
01 foreachvertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)05 S
06 Q.init(G.V())
07whilenotQ.isEmpty()08 u Q.extractMin()
09 S S {u}
10 foreachv u.adjacent() do11 Relax(u, v, G)
12 Q.modifyKey(v)
-
7/22/2019 Graph Theory Seminar
44/50
Dijkstras Example (3)
8 9
5 7
0
u v
yx
10
5
1
2 39
4 67
2
8 9
5 7
0
u v
yx
10
5
1
2 39
4 67
2
Dijkstra(G,s)
01 foreachvertex u G.V()
02 u.setd()
03 u.setparent(NIL)
04 s.setd(0)05 S
06 Q.init(G.V())
07whilenotQ.isEmpty()08 u Q.extractMin()
09 S S {u}
10 foreachv u.adjacent() do11 Relax(u, v, G)
12 Q.modifyKey(v)
-
7/22/2019 Graph Theory Seminar
45/50
Dijkstras Running Time
Extract-Min executed |V| time Decrease-Key executed |E| time
Time = |V| TExtract-Min+ |E| TDecrease-Key Tdepends on different Q implementations
Q T(Extract-
Min)
T(Decrease-Key) Total
array O(V) O(1) O(V 2)
binary heap O(lgV) O(lg V) O(Elg V)
Fibonacci heap O(lgV) O(1) (amort.) O(VlgV+ E)
46
-
7/22/2019 Graph Theory Seminar
46/50
Bellman-Ford Algorithm
Dijkstras doesnt work when there are negative edges:
Intuitionwe can not be greedy any more on the assumption that
the lengths of paths will only increase in the future
Bellman-Ford algorithm detects negative cycles (returns
false) or returns the shortest path-tree
-
7/22/2019 Graph Theory Seminar
47/50
Bellman-Ford Algorithm
Bellman-Ford(G,s)
01 foreachvertex u G.V()02 u.setd()03 u.setparent(NIL)
04 s.setd(0)
05fori 1 to |G.V()|-1 do06 foreach edge (u,v) G.E() do
07 Relax(u,v,G)
08 foreach edge (u,v) G.E() do
09 if v.d() > u.d() + G.w(u,v) then10 return false
11 return true
-
7/22/2019 Graph Theory Seminar
48/50
Bellman-Ford Example
5
0s
zy
6
7
8-3
72
9
-2xt
-4
6
7
0s
zy
6
7
8-3
72
9
-2xt
-4
5
6 4
7 2
0s
zy
6
7
8 -37
2
9
-2xt
-4
5
2 4
7 2
0s
zy
6
7
8 -37
2
9
-2xt
-4
5
-
7/22/2019 Graph Theory Seminar
49/50
Bellman-Ford Example
2 4
7 2
0s
zy
6
7
8-3
72
9
-2xt
-4
Bellman-Ford running time:
(|V|-1)|E| + |E| = Q(VE)
5
-
7/22/2019 Graph Theory Seminar
50/50
Thank you !