Professional Documents
Culture Documents
Lecture 22 - Graphs PDF
Lecture 22 - Graphs PDF
20. Graphs
20-Graphs 1
Graphs
20-Graphs 3
Graphs
• Associated with these vertices are |E| = 5 edges
– E = {{v1, v2}, {v3, v5}, {v4, v8}, {v4, v9}, {v6, v9}}
• Pair {vj , vk} indicates following relations
– Vertex vj is adjacent to vertex vk
– Vertex vk is adjacent to vertex vj
20-Graphs 4
Graphs – Example
• Given |V| = 7 vertices and |E| = 9 edges
– V = {A, B, C, D, E, F, G}
– E = {{A, B}, {A, D}, {A, E}, {B, C}, {B, D}, {B, E},
{C, E}, {C, F}, {D, E}}
20-Graphs 5
Applications Of Graphs
• Driving Map
– Vertex = Intersection, destinations
– Edge = Road
• Airline Traffic
– Vertex = Cities serviced by the airline
– Edge = Flight exists between two cities
• Computer networks
– Vertex = Server nodes, end devices, routers
– Edge = Data link
20-Graphs 6
Applications Of Graphs
20-Graphs 7
Undirected Graphs – Definition
• An undirected Graph is defined as G=(V,E) consisting of
– Set V of vertices: V = {v1, v2, ..., vn}
Number of vertices is denoted by |V| = n
– Set E of unordered pairs {vi, vj} termed edges
Edges connect the vertices
20-Graphs 8
Degree
20-Graphs 9
Path
20-Graphs 10
Path – Example
• Path of length 5: (A, B, E, C, B, D)
– Repitition of vertex B
20-Graphs 11
Path – Example
• A trivial path of length 0: (A)
20-Graphs 12
Path – Types
• A simple path has no repetitions other than perhaps the first and
last vertices
• A cycle is a simple path of at least two vertices with the first and
last vertices equal
– Note: these definitions are not universal
20-Graphs 13
Subgraph
20-Graphs 14
Connectedness
• Two vertices vi, vj are said to be connected if there exists a path
from vi to vj
• A graph is connected if there exists a path from every vertex to
every other vertex
20-Graphs 16
Weighted Graphs
20-Graphs 17
Weighted Graphs – Example
20-Graphs 18
Weighted Graphs – Example
20-Graphs 19
Trees
20-Graphs 22
Trees – Example
20-Graphs 23
Forest
20-Graphs 24
Directed Graphs
20-Graphs 25
Directed Graphs
• Given our graph of nine vertices V = {v1, v2, … v9}
– These six pairs (vj , vk) are directed edges
– E = {(v1, v2), (v3, v5), (v5, v3), (v6, v9), (v8, v4),
(v9, v4)}
20-Graphs 26
In and Out Degree
• In this graph:
– In-degree(v1) = 0 out-degree(v1) = 2
– In-degree(v5) = 2 out-degree(v5) = 3
20-Graphs 27
Path
20-Graphs 28
Connectedness
• Two vertices vj, vk are said to be connected if there exists a path
from vj to vk
– A graph is strongly connected if there exists a directed path between
any two vertices
– A graph is weakly connected if there exists a path between any two
vertices that ignores the direction
20-Graphs 29
Connectedness – Example
• The sub-graph {v3, v4, v5, v8} is strongly connected
• The sub-graph {v1, v2, v3, v4, v5, v8} is weakly connected
20-Graphs 30
Weighted Directed Graphs
6.4
7.5
6.7 6.8
7.3 5.9
4.1
3.2 4.7
5.4 4.5
20-Graphs 31
Representation
20-Graphs 32
Adjacency Matrix
• Two dimensional matrix of size n x n where n = |V|
• a[i, j] = 0 (F) if there is no edge between vertices vi and vj
• a[i, j] = 1 (T) if there is an edge between vertices vi and vj
• Adjacency matrix of undirected graphs is symmetric
– a[i, j] = a[j, i]
F
F
F
F
F
F
20-Graphs 33
Adjacency Matrix – Weighted Graph
• The matrix entry [j, k] is set to the weight of the edge (vj, vk)
• How to indicate absence of an edge in the graph?
20-Graphs 34
Adjacency Matrix – Directed Graph
20-Graphs 35
Adjacency Matrix – Analysis
1 2 3 4 5 6 7 8 9
1 1 1
2
3 1
4 1 1
5 1 1 1
6 1
7 1
8 1
9
20-Graphs 36
Adjacency Matrix – Problem
1 2 3 4 5 6 7 8 9
1 1 1
2
3 1
4 1 1
5 1 1 1
6 1
7 1
8 1
9
20-Graphs 37
Adjacency List
1 • →2→4
2 •
3 • → 5
4 • → 2→5
5 • → 2→3→8
6 • → 9
7 • → 9
8 • → 4
9 •
20-Graphs 38
Adjacency List – Weighted Graphs
Linked list
20-Graphs 39
Adjacency List – Implementation
class SingleNode {
private:
int adjacent_vertex;
double edge_weight;
SingleNode * next_node;
};
SingleNode * array;
array = new SingleNode[16];
20-Graphs 40
Graph Problems – Euler Tour
• Problem: To devise a walk through the city that would cross each
of the seven bridges of Königsberg once and only once
– Euler proved problem has no solution
20-Graphs 41
Graph Problems – Hamiltonian Cycle
20-Graphs 42
Graph Problems – Traveling Salesman
• A salesman wishes to
– Visit a number of towns, and then
– Return to his starting town
• Given the travelling times between towns, how should the travel be
planned, so that:
– He visits each town exactly once, and
– He travels in as short time as possible
20-Graphs 43
Graph Problems – Others
20-Graphs 44
Any Question So Far?
20-Graphs 45