GRAPH THEORY
Prof S Sameen Fatima
Dept of Computer Science and Engineering
Osmania University College of Engineering
Hyder...
OVERVIEW
• BASICS
• REPRESENTATION OF GRAPHS
• MINIMUM SPANNING TREE
• SEARCH ALGORITHMS
• EXAMPLES
Graph Theory S Sameen ...
BASICS
1. What Is a Graph?
2. Kinds of Graphs
3. Vertex Degree
4. Paths and Cycles
Graph Theory S Sameen Fatima 3
The KÖnigsberg Bridge Problem
• Königsber is a city on the Pregel river in
Prussia
• The city occupied two islands plus ar...
A Model
• A vertex : an island
• An edge : a path(bridge) between two
islands
Graph Theory S Sameen Fatima 5
e1
e2
e3
e4
e...
General Model
• A vertex : an object
• An edge : a relation between two objects
Graph Theory S Sameen Fatima 6
common
memb...
What Is a Graph?
• A graph G is an ordered pair (V, E)
consisting of:
– A vertex set V = {W, X, Y, Z}
– An edge set E = {e...
What Is a Graph?
• A graph, G is an ordered triple (V, E, f)
consisting of
– V is a set of nodes, points, or vertices.
– E...
Loop, Multiple edges
• Loop : An edge whose endpoints are
equal
• Multiple edges : Edges have the same
pair of endpoints
G...
Graph Theory S Sameen Fatima 10
Simple Graph
Simple graph : A graph has no loops or multiple edges
loop
Multiple
edges
It ...
Adjacent, neighbors
• Two vertices are adjacent and are
neighbors if they are the endpoints of an
edge
• Example:
– A and ...
Finite Graph, Null Graph
• Finite graph : an graph whose vertex set
and edge set are finite
• Null graph : the graph whose...
Connected and Disconnected
• Connected : There exists at least one
path between two vertices
• Disconnected : Otherwise
• ...
Complete Graph
• Complete Graph: A simple graph in which every
pair of vertices are adjacent
• If no of vertices = n, then...
Sparse/Dense Graph
• A graph is sparse if | E | | V |
• A graph is dense if | E | | V |2.
Graph Theory S Sameen Fatima 15
Directed Graph (digraph)
In a digraph edges have directions
Graph Theory S Sameen Fatima 16
Weighted Graph
Weighted graph is a graph for which each edge
has an associated weight, usually given by a
weight function ...
Planar Graph
• Can be drawn on a plane such that no two edges intersect
Graph Theory S Sameen Fatima 18
Complement
Complement of G: The complement G’ of
a simple graph G :
– A simple graph
– V(G’) = V(G)
– E(G’) = { uv | uv E(...
Subgraphs
• A subgraph of a graph G is a graph H
such that:
– V(H) V(G) and E(H) E(G) and
– The assignment of endpoints to...
Subgraphs
• Example: H1, H2, and H3 are subgraphs
of G
Graph Theory S Sameen Fatima 22
c
d
a b
de
a b
c
de
H1
G
H3
H2
a b
...
Bipartite Graphs
• A graph G is bipartite if V(G) is the union of
two disjoint independent sets called partite
sets of G
•...
Chromatic Number
• The chromatic number of a graph G,
written x(G), is the minimum number of
colors needed to label the ve...
Maps and coloring
• A map is a partition of the plane into
connected regions
• Can we color the regions of every map
using...
Scheduling and Graph Coloring
• Two committees can not hold meetings
at the same time if two committees have
common member...
Scheduling and Graph Coloring
• Model:
– One committee being represented by a
vertex
– An edge between two vertices if two...
Scheduling and Graph Coloring
• Scheduling problem is equivalent to
graph coloring problem
Graph Theory S Sameen Fatima 28...
Degree
Degree: Number of edges incident on a node
Graph Theory S Sameen Fatima 29
A
D E F
B C
The degree of B is 2.
Degree (Directed Graphs)
• In degree: Number of edges entering a node
• Out degree: Number of edges leaving a node
• Degre...
Degree: Simple Facts
• If G is a digraph with m edges, then
indeg(v) = outdeg(v) = m = |E |
• If G is a graph with m edges...
Path
• A path is a sequence of vertices such that there is
an edge from each vertex to its successor.
• A path is simple i...
Cycle
• A path from a vertex to itself is called a cycle.
• A graph is called cyclic if it contains a cycle;
– otherwise i...
Other Paths
• Geodesic path: shortest path
– Geodesic paths are not necessarily unique: It is quite possible to
have more ...
Euclerian Path
• An undirected graph possesses an Euclerian Path
if and only if it is connected and has either zero or
two...
GRAPH REPRESENTATION
• Adjacency Matrix
• Incidence Matrix
• Adjacency List
Graph Theory S Sameen Fatima 36
Adjacency, Incidence, and Degree
• Assume ei is an edge whose endpoints are (vj,vk)
• The vertices vj and vk are said to b...
Adjacency Matrix
• Let G = (V, E), |V| = n and |E|=m
• The adjacency matrix of G written A(G), is the
|V| x |V| matrix in ...
Adjacency Matrix
• Let G = (V, E), |V| = n and |E|=m
• The adjacency matrix of G written A(G), is the |V| x |V|
matrix in ...
Adjacency Matrix (Weighted Graph)
• Let G = (V, E), |V| = n and |E|=m
• The adjacency matrix of G written A(G), is the |V|...
Incidence Matrix
• Let G = (V, E), |V| = n and |E|=m
• The incidence matrix M(G) is the |V| x |E|
matrix in which entry mi...
Adjacency List Representation
• Adjacency-list representation
– an array of |V | elements, one for each vertex in V
– For ...
Adjacency List Representation
for a Digraph
Graph Theory S Sameen Fatima 43
1
5
1
22
5
4 4
3 3
2 5
5 3 4
4
5
5
Minimum Spanning Tree
Graph Theory S Sameen Fatima 55
Minimum Spanning Tree
• What is MST?
• Kruskal's Algorithm
• Prim's Algorithm
Graph Theory S Sameen Fatima 56
A spanning tree of a graph is just a subgraph that
contains all the vertices and is a tree.
A graph may have many spanning...
All 16 of its Spanning TreesComplete Graph
Graph Theory S Sameen Fatima 58
Minimum Spanning Trees
The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that
graph.
5
...
Kruskal's Algorithm
This algorithm creates a forest of trees. Initially the forest consists of n single
node trees (and no...
The steps are:
1. The forest is constructed - with each node in a separate tree.
2. The edges are placed in a priority que...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Complete Graph
Graph Theory S Sameen Fatima 62
1
4
2
5
2
5
4
3
4
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
A AB D
B B
B
C D
J C
C
E
F
D
D H
J E...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
...
4
1
2
2 1
3
32
4
A
B C
D
E F
G
H
I
J
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Minimum Spanning Tree Complete G...
Prim's Algorithm
This algorithm starts with one node. It then, one by one, adds a node that
is unconnected to the new grap...
The steps are:
1. The new graph is constructed - with one node from the old graph.
2. While new graph has fewer than n nod...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Complete Graph
Graph Theory S Sameen Fatima 80
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
I
J
Old Graph New Graph
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H
...
4
1
2
2 1
3
32
4
A
B C
D
E F
G
H
I
J
Complete Graph Minimum Spanning Tree
4
1
2 3
2 1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E F
G
H...
Examples
• Cost of wiring electronic components
• Shortest route between two cities.
• Shortest distance between all pairs...
Examples
• Flow of material
– liquid flowing through pipes
– current through electrical networks
– information through com...
Graph Algorithms
Graph Theory S Sameen Fatima 106
Search Algorithms
• Breadth First Search
• Depth Dirst Search
Graph Theory S Sameen Fatima 107
Breadth-First Search(BFS)
1. open  (initial state).
2. If open is empty , report failure , stop.
3. s  pop ( open )
4. I...
Space for BFS
Space is calculated in terms of open list.
In the worst case:
The solution may be the rightmost node at the ...
Time for BFS
In the worst case, the solution will be the right most node at depth
d, that is all the nodes would be expand...
Depth-First Search(DFS)
1. open  ( initial state )
2. If open is empty , report failure ,stop.
3. s  pop ( open )
4. If ...
Space for DFS
Space is calculated in terms of open list.
In the worst case:
At the last level (level d) the no. of nodes =...
Time for DFS
In the worst case, the solution will be the right most node at depth
d, that is all the nodes would be expand...
Depth-First Iterative Deepening
(DFID)
The depth-first iterative deepening algorithm combines the
advantage of low space r...
Time requirement for DFID
On a search to depth d =1, b nodes are visited. For d = 2 , b1 + b2 nodes are visited and so on....
Examples
• Shortest route between two cities.
• Shortest distance between all pairs of cities in
a road atlas.
• Matching ...
Examples
• Flow of material
– liquid flowing through pipes
– current through electrical networks
– information through com...
Thank you
Graph Theory S Sameen Fatima 118
Upcoming SlideShare
Loading in...5
×

graph theory

1,540

Published on

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,540
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
128
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "graph theory "

  1. 1. GRAPH THEORY Prof S Sameen Fatima Dept of Computer Science and Engineering Osmania University College of Engineering Hyderabad 500007 sameenf@gmail.com Graph Theory S Sameen Fatima 1
  2. 2. OVERVIEW • BASICS • REPRESENTATION OF GRAPHS • MINIMUM SPANNING TREE • SEARCH ALGORITHMS • EXAMPLES Graph Theory S Sameen Fatima 2
  3. 3. BASICS 1. What Is a Graph? 2. Kinds of Graphs 3. Vertex Degree 4. Paths and Cycles Graph Theory S Sameen Fatima 3
  4. 4. The KÖnigsberg Bridge Problem • Königsber is a city on the Pregel river in Prussia • The city occupied two islands plus areas on both banks • Problem: Whether they could leave home, cross every bridge exactly once, and return home. Graph Theory S Sameen Fatima 4 X Y Z W
  5. 5. A Model • A vertex : an island • An edge : a path(bridge) between two islands Graph Theory S Sameen Fatima 5 e1 e2 e3 e4 e6 e5 e7 Z Y X W X Y Z W
  6. 6. General Model • A vertex : an object • An edge : a relation between two objects Graph Theory S Sameen Fatima 6 common member Committee 1 Committee 2
  7. 7. What Is a Graph? • A graph G is an ordered pair (V, E) consisting of: – A vertex set V = {W, X, Y, Z} – An edge set E = {e1, e2, e3, e4, e5, e6, e7} Graph Theory S Sameen Fatima 7 e1 e2 e3 e4 e6 e5 e7 Z Y X W
  8. 8. What Is a Graph? • A graph, G is an ordered triple (V, E, f) consisting of – V is a set of nodes, points, or vertices. – E is a set, whose elements are known as edges or lines. – f is a function that maps each element of E to an unordered pair of vertices in V. Graph Theory S Sameen Fatima 8
  9. 9. Loop, Multiple edges • Loop : An edge whose endpoints are equal • Multiple edges : Edges have the same pair of endpoints Graph Theory S Sameen Fatima 9 loop Multiple edges
  10. 10. Graph Theory S Sameen Fatima 10 Simple Graph Simple graph : A graph has no loops or multiple edges loop Multiple edges It is not simple. It is a simple graph.
  11. 11. Adjacent, neighbors • Two vertices are adjacent and are neighbors if they are the endpoints of an edge • Example: – A and B are adjacent – A and D are not adjacent Graph Theory S Sameen Fatima 11 A B C D
  12. 12. Finite Graph, Null Graph • Finite graph : an graph whose vertex set and edge set are finite • Null graph : the graph whose vertex set and edges are empty Graph Theory S Sameen Fatima 12
  13. 13. Connected and Disconnected • Connected : There exists at least one path between two vertices • Disconnected : Otherwise • Example: – H1 and H2 are connected – H3 is disconnected Graph Theory S Sameen Fatima 13 c d a b de a b c d eH1 H3H2
  14. 14. Complete Graph • Complete Graph: A simple graph in which every pair of vertices are adjacent • If no of vertices = n, then there are n(n-1) edges Graph Theory S Sameen Fatima 14
  15. 15. Sparse/Dense Graph • A graph is sparse if | E | | V | • A graph is dense if | E | | V |2. Graph Theory S Sameen Fatima 15
  16. 16. Directed Graph (digraph) In a digraph edges have directions Graph Theory S Sameen Fatima 16
  17. 17. Weighted Graph Weighted graph is a graph for which each edge has an associated weight, usually given by a weight function w: E R. Graph Theory S Sameen Fatima 17 1 2 3 4 5 6 .5 1.2 .2 .5 1.5 .3 1 4 5 6 2 3 2 1 35
  18. 18. Planar Graph • Can be drawn on a plane such that no two edges intersect Graph Theory S Sameen Fatima 18
  19. 19. Complement Complement of G: The complement G’ of a simple graph G : – A simple graph – V(G’) = V(G) – E(G’) = { uv | uv E(G) } Graph Theory S Sameen Fatima 19 G u v w x y G’ u v wx y
  20. 20. Subgraphs • A subgraph of a graph G is a graph H such that: – V(H) V(G) and E(H) E(G) and – The assignment of endpoints to edges in H is the same as in G. Graph Theory S Sameen Fatima 21
  21. 21. Subgraphs • Example: H1, H2, and H3 are subgraphs of G Graph Theory S Sameen Fatima 22 c d a b de a b c de H1 G H3 H2 a b c de
  22. 22. Bipartite Graphs • A graph G is bipartite if V(G) is the union of two disjoint independent sets called partite sets of G • Also: The vertices can be partitioned into two sets such that each set is independent • Matching Problem • Job Assignment Problem Graph Theory S Sameen Fatima 23 Workers Jobs Boys Girls
  23. 23. Chromatic Number • The chromatic number of a graph G, written x(G), is the minimum number of colors needed to label the vertices so that adjacent vertices receive different colors Graph Theory S Sameen Fatima 24 Red Green Blue Blue x(G) = 3
  24. 24. Maps and coloring • A map is a partition of the plane into connected regions • Can we color the regions of every map using at most four colors so that neighboring regions have different colors? • Map Coloring graph coloring – A region A vertex – Adjacency An edge Graph Theory S Sameen Fatima 25
  25. 25. Scheduling and Graph Coloring • Two committees can not hold meetings at the same time if two committees have common member Graph Theory S Sameen Fatima 26 common member Committee 1 Committee 2
  26. 26. Scheduling and Graph Coloring • Model: – One committee being represented by a vertex – An edge between two vertices if two corresponding committees have common member – Two adjacent vertices can not receive the same color Graph Theory S Sameen Fatima 27 common member Committee 1 Committee 2
  27. 27. Scheduling and Graph Coloring • Scheduling problem is equivalent to graph coloring problem Graph Theory S Sameen Fatima 28 Common MemberCommittee 1 Committee 2 Committee 3 Common Member Different Color No Common Member Same Color OK Same time slot OK
  28. 28. Degree Degree: Number of edges incident on a node Graph Theory S Sameen Fatima 29 A D E F B C The degree of B is 2.
  29. 29. Degree (Directed Graphs) • In degree: Number of edges entering a node • Out degree: Number of edges leaving a node • Degree = Indegree + Outdegree Graph Theory S Sameen Fatima 30 1 2 4 5 The in degree of 2 is 2 and the out degree of 2 is 3.
  30. 30. Degree: Simple Facts • If G is a digraph with m edges, then indeg(v) = outdeg(v) = m = |E | • If G is a graph with m edges, then deg(v) = 2m = 2 |E | – Number of Odd degree Nodes is even Graph Theory S Sameen Fatima 31
  31. 31. Path • A path is a sequence of vertices such that there is an edge from each vertex to its successor. • A path is simple if each vertex is distinct. • A circuit is a path in which the terminal vertex coincides with the initial vertex Graph Theory S Sameen Fatima 32 1 2 3 4 5 6 Simple path: [ 1, 2, 4, 5 ] Path: [ 1, 2, 4, 5, 4] Circuit: [ 1, 2, 4, 5, 4, 1]
  32. 32. Cycle • A path from a vertex to itself is called a cycle. • A graph is called cyclic if it contains a cycle; – otherwise it is called acyclic Graph Theory S Sameen Fatima 33 1 2 3 4 5 6 Cycle
  33. 33. Other Paths • Geodesic path: shortest path – Geodesic paths are not necessarily unique: It is quite possible to have more than one path of equal length between a given pair of vertices – Diameter of a graph: the length of the longest geodesic path between any pair of vertices in the network for which a path actually exists • Eulerian path: a path that traverses each edge in a network exactly once The Königsberg bridge problem • Hamilton path: a path that visits each vertex in a network exactly once 34
  34. 34. Euclerian Path • An undirected graph possesses an Euclerian Path if and only if it is connected and has either zero or two vertices of odd degree OR • An undirected graph possesses an Euclerian Path if and only if it is connected and its vertices are all of even degree There is no Euclerian Path for the Konigsberg Bridge Problem Graph Theory S Sameen Fatima 35
  35. 35. GRAPH REPRESENTATION • Adjacency Matrix • Incidence Matrix • Adjacency List Graph Theory S Sameen Fatima 36
  36. 36. Adjacency, Incidence, and Degree • Assume ei is an edge whose endpoints are (vj,vk) • The vertices vj and vk are said to be adjacent • The edge ei is said to be incident upon vj • Degree of a vertex vk is the number of edges incident upon vk . It is denoted as d(vk) Graph Theory S Sameen Fatima 37 ei vj vk
  37. 37. Adjacency Matrix • Let G = (V, E), |V| = n and |E|=m • The adjacency matrix of G written A(G), is the |V| x |V| matrix in which entry ai,j is the number of edges in G with endpoints {vi, vj}. Graph Theory S Sameen Fatima 38 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
  38. 38. Adjacency Matrix • Let G = (V, E), |V| = n and |E|=m • The adjacency matrix of G written A(G), is the |V| x |V| matrix in which entry ai,j is 1 if an edge exists otherwise it is 0 Graph Theory S Sameen Fatima 39 1 5 2 4 3 1 2 3 4 5 1 2 3 4 5 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0
  39. 39. Adjacency Matrix (Weighted Graph) • Let G = (V, E), |V| = n and |E|=m • The adjacency matrix of G written A(G), is the |V| x |V| matrix in which entry ai,j is weight of the edge if it exists otherwise it is 0 Graph Theory S Sameen Fatima 40 1 5 2 4 3 1 2 3 4 5 1 2 3 4 5 0 5 0 0 1 5 0 4 6 3 0 4 0 2 0 0 6 2 0 7 1 3 0 7 0 5 3 7 61 2 4
  40. 40. Incidence Matrix • Let G = (V, E), |V| = n and |E|=m • The incidence matrix M(G) is the |V| x |E| matrix in which entry mi,j is 1 if vi is an endpoint of ei and otherwise is 0. Graph Theory S Sameen Fatima 41 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
  41. 41. Adjacency List Representation • Adjacency-list representation – an array of |V | elements, one for each vertex in V – For each u V , ADJ [ u ] points to all its adjacent vertices. Graph Theory S Sameen Fatima 42
  42. 42. Adjacency List Representation for a Digraph Graph Theory S Sameen Fatima 43 1 5 1 22 5 4 4 3 3 2 5 5 3 4 4 5 5
  43. 43. Minimum Spanning Tree Graph Theory S Sameen Fatima 55
  44. 44. Minimum Spanning Tree • What is MST? • Kruskal's Algorithm • Prim's Algorithm Graph Theory S Sameen Fatima 56
  45. 45. A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees. o r o r o r Some Spanning Trees from Graph AGraph A Spanning Trees Graph Theory S Sameen Fatima 57
  46. 46. All 16 of its Spanning TreesComplete Graph Graph Theory S Sameen Fatima 58
  47. 47. Minimum Spanning Trees The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph. 5 7 2 1 3 4 2 1 3 Complete Graph Minimum Spanning Tree Graph Theory S Sameen Fatima 59
  48. 48. Kruskal's Algorithm This algorithm creates a forest of trees. Initially the forest consists of n single node trees (and no edges). At each step, we add one edge (the cheapest one) so that it joins two trees together. If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed. Graph Theory S Sameen Fatima 60
  49. 49. The steps are: 1. The forest is constructed - with each node in a separate tree. 2. The edges are placed in a priority queue. 3. Until we've added n-1 edges, 1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two trees together. Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T. Graph Theory S Sameen Fatima 61
  50. 50. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Complete Graph Graph Theory S Sameen Fatima 62
  51. 51. 1 4 2 5 2 5 4 3 4 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J A AB D B B B C D J C C E F D D H J E G F FG I G GI J H J JI Graph Theory S Sameen Fatima 63
  52. 52. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize) Graph Theory S Sameen Fatima 64
  53. 53. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 65
  54. 54. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 66
  55. 55. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 67
  56. 56. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 68
  57. 57. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 69
  58. 58. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Cycle Don’t Add Edge Graph Theory S Sameen Fatima 70
  59. 59. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 71
  60. 60. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 72
  61. 61. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 73
  62. 62. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Cycle Don’t Add Edge Graph Theory S Sameen Fatima 74
  63. 63. 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J B B D J C C E F D D H J E G F F G I G G I J H J JI 1A D 4B C 4A B Add Edge Graph Theory S Sameen Fatima 75
  64. 64. 4 1 2 2 1 3 32 4 A B C D E F G H I J 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Minimum Spanning Tree Complete Graph Graph Theory S Sameen Fatima 76
  65. 65. Prim's Algorithm This algorithm starts with one node. It then, one by one, adds a node that is unconnected to the new graph, each time selecting the node whose connecting edge has the smallest weight out of the available nodes’ connecting edges. Graph Theory S Sameen Fatima 78
  66. 66. The steps are: 1. The new graph is constructed - with one node from the old graph. 2. While new graph has fewer than n nodes, 1. Find the node from the old graph with the smallest connecting edge to the new graph, 2. Add it to the new graph Every step will have joined one node, so that at the end we will have one graph with all the nodes and it will be a minimum spanning tree of the original graph. Graph Theory S Sameen Fatima 79
  67. 67. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Complete Graph Graph Theory S Sameen Fatima 80
  68. 68. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 81
  69. 69. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 82
  70. 70. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 83
  71. 71. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 84
  72. 72. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 85
  73. 73. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 86
  74. 74. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 87
  75. 75. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 88
  76. 76. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 89
  77. 77. 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Old Graph New Graph 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 90
  78. 78. 4 1 2 2 1 3 32 4 A B C D E F G H I J Complete Graph Minimum Spanning Tree 4 1 2 3 2 1 3 5 3 4 2 5 6 4 4 10 A B C D E F G H I J Graph Theory S Sameen Fatima 91
  79. 79. Examples • Cost of wiring electronic components • Shortest route between two cities. • Shortest distance between all pairs of cities in a road atlas. • Matching / Resource Allocation • Task scheduling Graph Theory S Sameen Fatima 101
  80. 80. Examples • Flow of material – liquid flowing through pipes – current through electrical networks – information through communication networks – parts through an assembly line • In Operating systems to model resource handling (deadlock problems) • In compilers for parsing and optimizing the code. Graph Theory S Sameen Fatima 102
  81. 81. Graph Algorithms Graph Theory S Sameen Fatima 106
  82. 82. Search Algorithms • Breadth First Search • Depth Dirst Search Graph Theory S Sameen Fatima 107
  83. 83. Breadth-First Search(BFS) 1. open  (initial state). 2. If open is empty , report failure , stop. 3. s  pop ( open ) 4. If s is a solution , report s, stop. 5. succs  successors(s). 6. Add succs to tail of open. 7. go to 2. Graph Theory S Sameen Fatima 108
  84. 84. Space for BFS Space is calculated in terms of open list. In the worst case: The solution may be the rightmost node at the last level At the last level (level d) the no. of nodes = bd Therefore, Total no: of nodes in the open list = bd Space O (bd) Graph Theory S Sameen Fatima 109
  85. 85. Time for BFS In the worst case, the solution will be the right most node at depth d, that is all the nodes would be expanded upto depth d. No. of nodes processed at 1st level = 1 No. of nodes processed at the 2nd level = b No: of nodes processed at the 3rd level = b2 ………….. No. of nodes processed at the dth level = bd Therefore, Total no. of nodes processed = 1 + b + b2 +……..bd. = b ( bd – 1 ) (b – 1 ) O ( bd ) (ignoring 1 in comparison to b) Graph Theory S Sameen Fatima 110
  86. 86. Depth-First Search(DFS) 1. open  ( initial state ) 2. If open is empty , report failure ,stop. 3. s  pop ( open ) 4. If s is a solution, report s, stop. 5. succs  successors (s). 6. add succs to head of open 7. go to 2. Graph Theory S Sameen Fatima 111
  87. 87. Space for DFS Space is calculated in terms of open list. In the worst case: At the last level (level d) the no. of nodes = b At each of the preceding (d-1) levels i.e., 1, 2, 3, …., (d-1), the no. of nodes = b-1 Therefore, Total no: of nodes at the preceding (d-1) levels = (d-1)(b-1) Space b+ (d-1) ( b-1) b + db – d – b + 1 d ( b – 1) + 1 bd O ( d ) ( In terms of open list ) Graph Theory S Sameen Fatima 112
  88. 88. Time for DFS In the worst case, the solution will be the right most node at depth d, that is all the nodes would be expanded upto depth d. No. of nodes processed at 1st level = 1 No. of nodes processed at the 2nd level = b No: of nodes processed at the 3rd level = b2 ………….. No. of nodes processed at the dth level = bd Therefore, Total no. of nodes processed = 1 + b + b2 +……..bd. = b ( bd – 1 ) (b – 1 ) O ( bd ) (ignoring 1 in comparison to b) Graph Theory S Sameen Fatima 113
  89. 89. Depth-First Iterative Deepening (DFID) The depth-first iterative deepening algorithm combines the advantage of low space requirement of depth first search (DFS) and advantage of finding an optimal solution of the breadth first search (BFS) time requirement, which is the same for both BFS and DFS 1. d  1 2. result  depth first (initial state, d) 3. (Comment: try to find a solution of length d using depth first search) 4. If result ≠ NIL, report it, stop 5. d  d+ 1 6. go to 2 Graph Theory S Sameen Fatima 114
  90. 90. Time requirement for DFID On a search to depth d =1, b nodes are visited. For d = 2 , b1 + b2 nodes are visited and so on. At d = k, b1 + b2 + …….. + bk nodes are visited Let’s define the cost of DFID as a recurrence relation DFID(1) = b1 DFID(k) = + DFID(k-1) This expands to bk + bk-1 + bk-2 + ………………….. + b1 bk-1 + bk-2 + ………………….. + b1 bk-2 + ………………….. + b1 …… ……. …….. ___________________________________________________________________ bk +2bk-1 +3bk-2 + ………………….. +kb1 = bk For large k the above expression asymptotes to bk (as the expression in the parenthesis asymptotes to 1). Hence time required for DFID is O(bk) Graph Theory S Sameen Fatima 115
  91. 91. Examples • Shortest route between two cities. • Shortest distance between all pairs of cities in a road atlas. • Matching / Resource Allocation • Task scheduling • Cost of wiring electronic components • Visibility / Coverage Graph Theory S Sameen Fatima 116
  92. 92. Examples • Flow of material – liquid flowing through pipes – current through electrical networks – information through communication networks – parts through an assembly line • In Operating systems to model resource handling (deadlock problems) • In compilers for parsing and optimizing the code. Graph Theory S Sameen Fatima 117
  93. 93. Thank you Graph Theory S Sameen Fatima 118
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×