1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
-
Upload
prosper-bates -
Category
Documents
-
view
218 -
download
1
Transcript of 1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
1
Abstract Data Type
We have discussed:
List
Tree
Today we will talk about Graph
2
Introduction to Graphs
Several problems in the real-world can be formulated as objects and connections between them.e.g.
What is the fastest way to get from city A to city B?
What is the cheapest way to get from city A to city B?
• We need to represent :
cities and distances and
cities and costs respectively.
3
Northwest Airline Flight
Boston
Hartford
Atlanta
Minneapolis
Austin
SF
Seattle
Anchorage
4
Introduction to Graphs
Graphs are so important as structures that they deserve their own field called graph theory
Graph theory is a branch of combinatorial mathematics and has been studied for hundreds of years.
Our goal is to study graphs from the algorithmic point of view.
5
Introduction to Graphs
Campus map Traveling salesperson Circuit layout
Project scheduling Oil flow
In many cases we are faced with a problem that is defined in terms of a number of objects that have some relationship among them. We then try to answer interesting questions.
Graphs are the basic mathematical formulation we use to tackle such problems.
6
Introduction to Graphs Are all the circuits elements in a computer connected?
• This can be answered by looking at the model representing the circuits (objects) and the wiring of the circuit (connections)
Job scheduling. Some tasks have to be processed in some pre-defined order
• Graphs can be used to model the order jobs are processed
7
Computer Network Or Internet
MCI
Regional NetworkComposed of other
networks
IntelCampusUmass
AT&T
8
Application
Salesman’s Route Postman’s Route
Shortest route for salesman, we call it the Traveling Salesman Problem
Start
9
Course Prerequisites
CSC122
Freshman Sophomore Junior Senior
CSC242
10
Nomenclature of Graphs
A graph is a collection of vertices (nodes) and edges (arcs)
Nodes are objects that have names and other properties
Edges normally don't have properties but sometimes they are assigned a value – e.g. a weight.
A graph can be drawn by marking points and linking them with a line
11
Applications --- Communication Network
Vertex = city, edge = communication link.
1
23
810
4 5 9 11
67
Vertex
Edge
12
Graph Definition
G = (V, E) Graph = (Vertices, Edges)
V is the vertex set
Vertices are also called nodes or points
E is the edge set
Each edge connects two vertices
13
Concepts of Graphs
edges (weight)
node or vertex
14
Two representations of the same graph
A B
C D
E
F
A
B
C
D E
F
A graph is an object that is independent of its representation (in the computer or a pictorial)
15
Graphs and their shape
In some applications the shape is used to better express the graph.
If we're representing cities as they appear in the map,
keep the shape of the map
Still, changing it will not interfere in the solution if you do.
Connected Nodes
Solutions base themselves on the
list of pairs of nodes pairs of nodes
that are connectedconnected in the graph
16
17
Number of Edges
V is the number of nodes (vertices) in a graph and
E represents the number of edges
The number of edges in a graph can range from
0 to V(V-1)/2.
We call a sparse graph a graph in which E is much less than V(V-1)/2.
18
Sparse Graph
For example: if
N = 4 - then
E = N-1= O(E)
(E = 3)
Where E is the # of Edges
• There are a very small number of edges in a
sparse graph where N is the number of vertices.
GRAPHS Definitions
We call a dense a graph in which the number of edges is close to the maximal number of edges.
We call a complete graph a graph in which E is exactly
V(V-1) / 2V(V-1) / 2
19
20
Complete Graph
Total number of edges in a complete graph:
E = N(N-1)/2
if N = 4, E = 6
• There is an edge between any two vertices
21
Definitions
A path from node A to node B in a graph is a list of successive connected nodes in the graph
A graph is connected if there is a path from every node to every other node in the graph
A graph that is not connected is made up of several connected components
A B
C D
E
F
A path from C to FA path from C to F
22
What is a Graph?
A Graph G consists of a set V of vertices or nodes and
a set E of edges that connect the vertices.
We write G=(V,E).
v1
v5
v4
v2
v3
e1
e4
e3
e2
G=(V,E)V={v1,v2,v3,v4,v5}E={e1,e2,e3,e4}e1=(v1,v2)e2=(v2,v3)e3=(v2,v4)e4=(v3,v5)
23
v1
v5v4
v2
v3 e2
e1
G=(V,E)
V={v1,v2,v3,v4,v5}
E={e1,e2}
e1=(v2,v3)e2=(v2,v4)
Graphs --- Examples
Note that not all vertices are connected
24
v1
v4
v2
v3
e1
e5
e4
e2
e3
e6
A Complete Graph V(V-1)/2
Fully connected
25
Directed Graphs
In some cases we want the edges to have directions associated with them:
we call such a graph a directed graph or a digraph.
G=(V,E)V={v1,v2,v3,v4}E={e1,e2,e3,e4}e1=(v2,v1)e2=(v1,v3)e3=(v1,v4)e4=(v4,v3)e5=(v3,v4)
v1
v4
v2v3
e1
e4
e2
e3
ordered pair(predecessor, successor)
26
Undirected vs. Directed Graph
Undirected Graph
– no oriented edge
Directed Graph
– every edge has oriented vertex
27
Weighted Graphs
In some cases, we want to associate a weight with each edge in the graph. Such a graph is known as a weighted graph.
Directed graphs(as well as undirected) can also be weighted (directed weighted graphs).
G=(V,E)V={v1,v2,v3,v4}E={e1,e2,e3,e4,e5}…………………..…………………..
v1
v4
v2
v3
40
63
15
50
75
55
28
Weighted Graph
Weighted graph: a graph with numbers assigned to its edges
Weight: cost, distance, travel time, hop, etc.
0
1
3
2
20 10
1
54
29
Sub-graph
Sub-graph: subset of vertices and edges
MORE TERMS
A simple path is a path in which no nodes are repeated(A C E)
A cycle is a path that is simple except that the first and the last nodes are the same. (ABDA)
It is a path from a node back to itself.
30
31
Simple Path
A simple path traverses a node no more than once ABCD is a simple path - its length is 3
B
C
D
A
path
32
Cycle –
A cycle is a path that starts and ends at the same point
CBDC is a simple cycle.
B
C
D
A
33
More Definitions
A graph with no cycles is called a tree
A spanning tree of a graph is a connected subgraph that contains all the nodes of the graph but has no cycles (a tree by definition)
A
B
C
D E
F
A
B
C
D E
F
GraphGraph
SpanningTree
SpanningTree
CycleCycle
34
More Graph Terminology
A vertex vj is said to be adjacent to a different vertex vi if an edge connects vi to vj, i.e.,
e=(vi,vj).
The length of a path is the number of edges in it.
A graph with no cycles is called an acyclic graph.
A directed acyclic graph is called a DAG.
35
More Graph Terminology
Two different vertices are connected if there is a path between them.
The degree of a vertex is the number of edges connected to it.
C
D
A
The degree of C is 2 – it has 2 edges
36
Connected vs. Unconnected Graph
Connected Graph Unconnected Graph
37
Directed Acyclic Graph
Directed Acyclic Graph (DAG) : directed graph without cycle
Examples Course Requirement Graph: DAG
38
Directed Acyclic Graph
D
C
B
A
This is NOT a DAG(Directed Acyclic Graph)
ABCD is a cycle in a directed graph
39
Graph Representation- Matrix
There are several ways to represent a graph in a computer program.
The two most common ways are using a• matrix or a list:
An Adjacency-matrix representation:
• Used when the graph is dense
Adjacency-matrix representation:
Adjacency-matrix representation:
•In the representation of a graph G=(V,E),
• it is assumed that nodes are numbered 1,2,3,...,V.
•The matrix representation consists of a matrix Ai,j
•This matrix is normally symmetric along the main diagonal
40
41
Adjacency Matrix
We have N = 20 nodes in graph
Use a Matrix A[0…N-1][0…N-1] or A[N][N]
if vertex i and vertex j are adjacent in graph, A[i][ j ] = 1,
otherwise A[i][ j ] = 0
if vertex i has a loop, A[ i ][ i ] = 1
if vertex i has no loop, A[ i ][ i ] = 0
42
Adjacency Matrix--Example 1
1
4
5
6
3
2
8
7
0
0 0 1 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7
0
1
2
3
4
7
5
6
0
0
0
0
0
1
0
0
8
0 0 0 0 0 0 0 08 0
Arrows mark connections
43
Example of Adjacency Matrix
0
1
3
2
A[i][j] 0 1 2 3
0 0 1 1 0
1 1 0 1 1
2 1 1 0 1
3 0 1 1 0
So, Matrix A =
0 1 1 0
1 0 1 1
1 1 0 1
0 1 1 0
44
Adjacency Matrix --- Example 2
8 6
8 9 9 6
0 1 2 3
0
1
2
323
0
16
8
9
The matrix is symmetric for undirected graphs.
A[2][1] =A[1][2]
45
Undirected vs. Directed
Undirected graph
adjacency matrix is symmetric
A[i][j]=A[j][i]
Directed graph
adjacency matrix may not be symmetric
A[i][j] A[j][i]
46
Directed Graph Matrix RepresentationNot symmetric
A[i][j] 0 1 2 3
0 0 1 1 1
1 0 0 0 1
2 0 0 0 1
3 0 0 0 0
0
1
3
2
So, Matrix A =
0 1 1 1
0 0 0 1
0 0 0 1
0 0 0 0
A[2][3] A[3][2]
47
Weighted Graph – Use weights in cells
A[i][j] 0 1 2 3
0 0 10 20 1
1 10 0 0 4
2 20 0 0 5
3 1 4 5 00
1
3
2
20 10
1
54
So, Matrix A =
0 10 20 1
10 0 0 4
20 0 0 5
1 4 5 0
48
Graph Representation
Another way to represent a graph uses an array of linked lists:
Adjacency-list representation:
This is preferred when the graph is sparse.
Adjacency-list representation:
The adjacency list for a graph with N vertices consists of
N Linked Lists.
The ith linked list has a node for vertex j if and only if the graph contains an edge from vertex i to vertex j.
In other words, AdjList[i] is a list of all nodes adjacent to i in the graph
49
50
Adjacency List --- Example 1
1
4
5
6
3
2
8
7
0
0
1
2
3
4
7
5
6
8
2 5
6
6
4
5
3 7
2 8
AdjList[]
51
Adjacency List
An adjacency list is as an array of linked lists
the ith element of the array is a list of vertices that
connect to vertex i
.
52
Example of Adjacency List
0
1
3
2
0
1
2
3
1 2 3
3
3
vertex 0 connect to vertex 1, 2 and 3
vertex 1 connects to 3
vertex 2 connects to 3
53
Weighted Graph
Weighted graph: extend each node with an additional field:
the weight
0
1
3
2
20 10
1
54
0
1
2
3
1 10 2 20 3 1
0 10 3 4
0 20 3 5
0 1 1 4 2 5
54
Adjacency List -Weighted --- Example 2
23
0
16
8
9
0
1
2
3
1 8 3 6
0 8 2 9
1 9
0 6
55
Space Requirement of Representations
Which one is better?
Memory space: adjacency matrix O(N2) adjacency list O(E) where E is # of
edges
Sparse graph adjacency list is better
Dense graph same running time for both
56
Comparison Of Representations where degree is the number of connected nodes
Cost Adjacency Matrix Adjacency List
Given two vertices u and v: Do a LookUP -
find out whether u and v are adjacent
O(1)
Find M[u][v]
degree of node
O(N)
Given a vertex u:
enumerate all neighbors of u
O(N)
Go through whole row
degree of node
O(N)
For all vertices:
enumerate all neighbors of each vertexO(N2)
Summations of all node degrees
O(E)
57
Which is Better?
Operation 1: Is there an edge from vertex i to vertex j? Operation 2: Find all vertices adjacent to vertex i.
Time:
Operation 1 Edge?
Operation 2 Find?
O(1) Search ListO(d)
Traverse ListO(d)
Traverse rowO(n)
Matrix List
Determine which operation is most frequent.
58
Which is Better?
Space:
O(n2)Matrix:
List: n + O(|E|) where E is number of edges AND N IS # OF NODES
O(n+|E|) = O(|V| + |E|)
OR THE SUM OF THE # OF EDGES AND NODES
59
Graph Traversal Algorithms
Two algorithms
Breadth First Traversal - uses a queue
Depth First Traversal - uses a stack
60
Breadth-first Search (BFS)
The breadth-first search in graphs will make use of shadings to identify nodes that have been visited
White nodes are nodes not yet visited
Gray nodes have been discovered but not visited, that is, not considered in the order of the breadth-first traversal
Black nodes have been visited
In the end of the breadth first search, a tree is formed.
This tree is called BFS tree.
61
Breadth-first Search (Start = A ) (Queue)
A B
C D
E
F
G
H
A B
C D
E
F
G
H
62
A B
C D F
G
H
63
Breadth-first Search-First In - is – First out
A B
C D
E
F
G
H
AAQUEUE
Take out A and put in its children- C,D,B
Put in A to start - we will use a Queue
OUTPUT: A
64
Breadth-first Search- First in First out
A B
C D
E
F
G
H
CCQUEUE DD BB
Take out C and put in its children: E,F
OUTPUT: A C
65
Breadth-first Search First In - is – First out
A B
C D
E
F
G
H
DDQUEUE BB EE FF
Take out D and put in its children- all have been visited.
OUTPUT: A C D
66
Breadth-first Search
A B
C D
E
F
G
H
BBQUEUE EE FFTake out B and put in its children:G
OUTPUT: A C D B
67
Breadth-first Search
A B
C D
E
F
G
H
EEQUEUE FF GG
Take out E and put in its children – no unvisited children
OUTPUT: A C D B E
68
Breadth-first Search
A B
C D
E
F
G
H
FFQUEUE GG
Take out F and put in its children
OUTPUT: A C D B E F
69
Breadth-first Search
A B
C D
E
F
G
H
GGQUEUE HH
Take out G and put in its children
OUTPUT: A C D B E F G
70
Breadth-first Search
A B
C D
E
F
G
H
HHQUEUE Take out H and Queue is empty
OUTPUT: A C D B E F G H
71
Breadth-first Search
A B
C D
E
F
G
H
QUEUE is empty
DEPTH FIRST SEARCH
The other major graph traversal is Depth First Search Depth First Search which searches deeper into the graphdeeper into the graph.
Now the ADT used is a stack ADT used is a stack - with the last one in is the first one last one in is the first one out approach.out approach.
The children of the first node are put in the stack, then the last child added is removed and its children are put in.
Then last of the child’s children is taken out and its children are put in. And so on……..
72
73
Depth-first Search (Start = A)- Use Stack
A B
C D
E
F
G
H
OUTPUT: A
74
Depth-first Search
A B
C D
E
F
G
H
AASTACKTake out A and put in its children: C, D, B
OUTPUT: A
75
Depth-first Search
A B
C D
E
F
G
H
CCSTACK DD BB
Take out B and put in its unvisited children : G
OUTPUT: A B
76
Depth-first Search
A B
C D
E
F
G
H
CCSTACK DD GG
Take out G and put in its unvisited children :H
OUTPUT: A B G
77
Depth-first Search
A B
C D
E
F
G
H
CCSTACK DD HHTake out H and put in its unvisited children :F
OUTPUT: A B G H
78
Depth-first Search
A B
C D
E
F
G
H
CCSTACK DD FF
Take out F and put in its children- no unvisited children
OUTPUT: A B G H F
79
Depth-first Search
A B
C D
E
F
G
H
CCSTACK DDTake out D and put in its children – no unvisited children
OUTPUT: A B G H F D
80
Depth-first Search
A B
C D
E
F
G
H
CCSTACK
Take out C and ADD E
OUTPUT: A B G H F D C
81
Depth-first Search
A B
C D
E
F
G
H
EESTACK
Take out E and put in its children – no unvisited children and Stack is empty
OUTPUT: A B G H F D C E
82
Depth-first Search
A B
C D
E
F
G
H
STACK is emptyOUTPUT: A B G H F D C E
83
Graph Traversals --- Basic Algorithm
Depth First Search = structure used for storagewhile (An un-VISITED vertex exists) { Initialize vertex vI ;
mark it as VISITED
while ( stack is not empty) { remove a vertex vj from stack visit vj
add un-VISITED vertices adjacent to vj to stack; mark each as VISITED }}
84
Recursive Depth-First Algorithm
1
4
56
3
2
DepthFirstSearch(vi)
{ visit vj; mark vi as VISITED
for each un-VISITED vertex vj adjacent to vi
DepthFirstSearch (vj) }
85
Graph Traversal
List out all cities that United Airlines can reach from Hartford Airport
CHI
LA
SF
NYC
Hartford
W. DC
86
Demos For Traversal Algorithm
Step 1: { Hartford } find neighbors of Hartford { Hartford, NYC, CHI }
CHI
NYC
LA
SF Hartford
W. DC
87
Demos For Traversal Algorithm
Step 2: { Hartford, NYC, CHI } find neighbors of NYC, CHI NYC goes to LA, CHI goes to SF
{ Hartford, NYC, CHI, LA, SF }CHI
NYC
LA
SF Hartford
W. DC
88
Demos For Traversal Algorithm
Step 3: {Hartford, NYC, CHI, LA, SF } find neighbors of LA, SF no other new neighbors
CHI
NYC
LA
SF Hartford
W. DC
89
Demos For Traversal Algorithm
Finally we get all cities that United Airlines can reach from Hartford Airport {Hartford, NYC, CHI, LA, SF }
CHI
NYC
LA
SF Hartford
W. DC
90
Graph Traversals
Traversal: visit each vertex in the graph once and only onceTraversal: visit each vertex in the graph once and only once. This is an a matrix of vertices.
1
4
56
3
2
79
8
1
1
1 1
1 2 3
1
4
1
2
3
1
1
5
1
14 1
6
1 1 15 1
7 8
16 1
7 1
8 1
9
1
1
9 1 1
91
Elementary Graph Operations
Graph traversals provide the basis for many elementary graph operations:
Spanning trees on graphsSpanning trees on graphs
Graph cycles Graph cycles
Connected components of a graphConnected components of a graph