Chapter4 - File Processing

Download Report

Transcript Chapter4 - File Processing

Chapter 10
Graph
Aree Teeraparbseree, Ph.D
1
What is Graph?

A graph is a collection of nodes called vertices, and the
connections between them, called edges.

A graph G = (V, E) is composed of:
V: set of vertices
E: set of edges
An edge e = (v1,v2) is a pair of vertices

A
e4
e1
e5
C
e3
B
e2
D
G = (V, E)
V(G) = {A, B, C, D}
E(G) = {e1, e2, e3, e4, e5}
2
Graphs examples
A
D
e4
e1
B
e5
C
e3
B
C
e2
A
D
e1
A
C
e3
E
A
B
B
D
e2
D
e6
G
e4
C
E
e5
F
F
3
Applications
CS16

Electronic circuits

Networks (roads, flights, communications)
JFK
LAX
LAX
STL
HNL
DFW
FTL
4
Directed vs. Undirected Graph

<B, C> != <C,B>
Directed graph
B is adjacent to C
C is adjacent from B
D
B
C

Undirected graph
D
(B, C) = (C,B)
B and C are adjacent
B
C
5
Edge

Parallel / Multiple Edges
A

B
A
B
Loop
A
6
Simple vs. Multi Graph

Simple Graph
A
e4
e1
e5
C
e3
B
e2
D

Multi Graph
A
e4
e6
e1
e5
C
e3
Loop
e4
B
e2
D
C
A
e3
e1
e5
e6
D
Parallel edge
B
e2
7
Identical graph
A
Graph G
e4
C
e5
e1
B
e6
e3
e4
e2
A
e1
e5
C
e3
D

e6
Graph H
B
e2
D
Graph G and graph H are identical
when
V(G) = V(H) and E(G) = E(H)
8
Vertex Degree

Degree of a vertex, deg(v) = number of
edge ends at that vertex
A
e4
e6
deg(A) = 3
e1
e5
C
e3
B
e2
deg(B) = 2
deg(C) = 4
deg(D) = 3
D
total degree = 2 * (e1:e6)
(3+2+4+3) = 2 *
6
Handshaking lemma
9
Path

Path = sequence of vertices v1,v2,. . .vk such that
consecutive vertices vi and vi+1 are adjacent.
a
b
c
d
a
e
b
a
c
c
e
d
abedc
b
e
d
bedc
10
Simple vs. Cycle path

Simple path
a
b
c
e
d
bedc

Cycle
a
b
c
e
d
cedc
11
Connected Graph

A graph G is connected if there is a path
in G between any given pair of vertices.
Connected graph
Disconnected graph
•12
12
Circuit

Circuit: a path which ends at the vertex it begins (so
a loop is an circuit of length one).

Euler circuit:


Passes along each edge once and only once (to visit all nodes)
Hamilton circuit:

visits each vertex once and only once (to visit all nodes).
a
b
b
c
c
d
e
d
Euler circuit
e
Hamilton circuit
13
Euler circuit

Euler graph is a connected graph with all
vertices in the graph have an even degree.
a
b
Odd degree
c
Disconnect
14
Tree

tree - connected graph without cycles.
15
Graph Representations
Adjacency Matrix
 Adjacency Lists

16
Adjacency Matrix

Let G=(V,E) be a graph with n vertices.

The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric



17
4
Ex. Adjacency Matrix
0
0
1
2
1 2
0 0 1
0
1
3
1 1


1 1 0 1 1 
2 1 1 0 1 


3 1 1 1 0
2
0 1 2
0 0 1 0


1 1 0 1 
2 0 0 0 
G2
3
symmetric
7
0 1 2 3 4 5 6 7
0 0 1 1

1 1
2 1

3 0
4 0
G1
6
1
2
3
0
5

5 0
6 0

7 0
0
0
1
0
0
0
0
0
0
1
0
0
0
0
G3
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0

0
0

0
1

0
0
0
0
0
0
1
0
1
18
Adjacency Lists (data structures)
Each row in adjacency matrix is represented as an adjacency list.
#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];
19
4
0
0
1
2
1
0
0
2
2
1
1
0
G1
0
1
2
1
0
0
2
G2
3
3
3
2
1
2
6
1
2
3
0
1
2
3
5
3
7
1
0
1
2
3
4
5
6
7
0
2
3
0
1
3
2
5
4
5
6
7
6
G3
20