8/3/2019 Lec-31&32 Graph
1/22
Graphs
8/3/2019 Lec-31&32 Graph
2/22
Graph Definitions A graph G is denoted by G = (V, E) where
V is the set ofvertices or nodes of the graph
E is the set ofedges or arcs connecting the vertices in
V
Each edge E is denoted as a pair (v,w) where v,wI
VFor example in the graph below
1
2
5
6 4
3
V = {1, 2, 3, 4, 5, 6}
E = {(1, 2) (2, 5) (3, 6) (4, 6)(5, 6)}
This is an example of anunorderedorundirectedgraph
8/3/2019 Lec-31&32 Graph
3/22
Graph Definitions (contd.)
If the pair of vertices is ordered then thegraph is a directedgraph or a di-graph
1
2
5
6 4
3 Here,
V = {1, 2, 3, 4, 5, 6}E = {(1, 2) (2, 5) (5, 6) (6,
3) (6, 4)}
Vertex vis adjacentto wiff (v,w) I E Sometimes an edge has another component
called a weightorcost. If the weight is absent
it is assumed to be 1
8/3/2019 Lec-31&32 Graph
4/22
Graph Definitions: PathApath is a sequence of vertices w1, w2, w3,
....wn such that (wi, wi+1) I E
Length of a path = # edges in the path
A loo
p is an edge from a vertex onto itself. Itis denoted by (v, v)
A simple path is a path where no vertices
are repeated along the path
A cycle is a path with at least one edge such
that the first and last vertices are the same,
i.e. w1 = wn
8/3/2019 Lec-31&32 Graph
5/22
Graph Definitions:
Connectedness
A graph is said to be connectedif there is a pathfrom every vertex to every other vertex
A connected graph is strongly connectedif it is aconnected graph as well as a directed graph
A connected graph is weakly connected if it is a directed graph that is not strongly connected, but,
the underlying undirected graph is connected
1
2
5
6 4
3
1
2
5
6 4
3
Strong or Weak?
8/3/2019 Lec-31&32 Graph
6/22
Applications of Graphs
Driving Map Edge = Road
Vertex = Intersection
Edge weight = Time required to cover the road
Airline Traffic Vertex = Cities serviced by the airline
Edge = Flight exists between two cities
Edge weight = Flight time or flight cost or both
Computer networks Vertex = Server nodes
Edge = Data link
Edge weight = Connection speed
CAD/VLSI
8/3/2019 Lec-31&32 Graph
7/22
Representing Graphs: Adjacency Matrix Adjacency Matrix
Two dimensional matrix of size n x n where n is thenumber of vertices in the graph
a[i, j] = 0 if there is no edge between vertices i and j
a[i, j] = 1 if there is an edge between vertices i and j
Undirected graphs have both a[i, j] and a[j, i] = 1 ifthere is an edge between vertices i and j
a[i, j] = weight for weighted graphs
Space requirement is 5(N2)
Problem: The array is very sparsely populated. Forexample if a directed graph has 4 vertices and 3edges, the adjacency matrix has 16 cells only 3 ofwhich are 1
8/3/2019 Lec-31&32 Graph
8/22
Representing Graphs: Adjacency
ListAdjacency List
Array of lists
Each vertex has an array entry
A vertex w is inserted in the list for vertex v ifthere is an outgoing edge from v to w
Space requirement = 5(E+V)
Sometimes, a hash-table of lists is used toimplement the adjacency list when the verticesare identified by a name (string) instead of aninteger
8/3/2019 Lec-31&32 Graph
9/22
Adjacency List Example1
2
5
6 4
3
1
2
3
4
5
6
2
5
6
43
Graph Adjacency List
An adjacency list for a weighted graph shouldcontain two elements in the list nodes oneelement for the vertex and the second element forthe weight of that edge
8/3/2019 Lec-31&32 Graph
10/22
Negative Cost Cycle A negative cost cycle is a cycle such that thesum of the costs of the edges is negative
The more we cycle through a negative cost
cycle, the lower the cost of the cycle becomes
2
3
4
4 2
-12
Cost of the path v1-v5 No traversal of cycle: 6
One traversal of cycle: 0
Two traversals of cycle: -6 Three traversals of cycle: -12
...
5
1
5
1
Negative cost cycles are not allowed when we traverse a graph to
find the weighted shortest path
8/3/2019 Lec-31&32 Graph
11/22
Application of Graphs
Using a model to solve aUsing a model to solve acomplicated traffic light problemcomplicated traffic light problem GIVEN: A complex intersection.
OBJECTIVE:Traffic light with minimum phases.
SOLUTION:
Identify permitted turns, going straight is a turn.
Make group of permitted turns.
Make the smallest possible number of groups.
Assign each phase of the traffic light to a group.
8/3/2019 Lec-31&32 Graph
12/22
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problem
AB
C
D
E
An intersection
8/3/2019 Lec-31&32 Graph
13/22
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problem
Roads C and E are one way, others are two way.
There are 13 permitted turns.
Some turns such as AB (from A to B) and EC can be carriedout simultaneously.
Other like AD and EB cross each other and can not be carried
out simultaneously.The traffic light should permit AB and EC simultaneously, butshould not allow AD and EB.
8/3/2019 Lec-31&32 Graph
14/22
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problem
AB
C
D
E
An intersection
AB & AC
AD & EB
8/3/2019 Lec-31&32 Graph
15/22
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problem
SOLUTION:
We model the problem using a structure called graph G(V,E).
A graph consists of a set of points called vertices, and linesconnecting the points, called edges.
Drawing a graph such that the vertices represent turns.
Edges between those turns that can NOT be performedsimultaneously.
8/3/2019 Lec-31&32 Graph
16/22
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problem
AB
C
D
E
An intersection
AB AC AD
BA BC BD
DA DB DC
EA EB EC ED
Partial graph of incompatible turns
8/3/2019 Lec-31&32 Graph
17/22
Partial table of incompatible turns
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problem
8/3/2019 Lec-31&32 Graph
18/22
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problem
SOLUTION:
The graph can aid in solving our problem.
A coloringof a graph is an assignment of a color to eachvertex of the graph, so that no two vertices connected byan edge have the same color.
Our problem is of coloring the graph of incompatibleturns using as few colors as possible.
8/3/2019 Lec-31&32 Graph
19/22
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problemMore on SOLUTION (Graph coloring):
The problem has been studied for decades.
The theory of algorithms tells us a lot about it.
Unfortunately this belongs to a class of problems calledas NP-Complete problems.
For such problems, all known solutions are basically tryall possibilities
In case of coloring, try all assignments of colors.
8/3/2019 Lec-31&32 Graph
20/22
Using a model to solve a complicatedUsing a model to solve a complicated
traffic light problemtraffic light problem
Approaches to attempting NP-Complete problems:
1. If the problem is small, might attempt to find anoptimal solution exhaustively.
2.Look for additional information about the problem.
3.Change the problem a little, and look for a good, butnot necessarily optimal solution.
An algorithm that quickly produces good but notnecessarily optimal solutions is called a heuristic.
8/3/2019 Lec-31&32 Graph
21/22
A reasonable heuristic for graph coloring is the greedyalgorithm.
Try to color as many vertices as possible with the firstcolor, and then as many uncolored vertices with thesecond color, and so on.
The approach would be:
1. Select some uncolored vertex, and color with new color.
2. Scan the list of uncolored vertices. For each uncoloredvertex, determine whether it has an edge to any vertexalready colored with the new color. If there is no suchedge, color the present vertices with the new color.
8/3/2019 Lec-31&32 Graph
22/22
This is called greedy, because it colors a vertex,whenever it can, without considering potentialdrawbacks.
1 5
3
4
21 2
3
4
5
1 5
3
1
3
5 2
4