7
1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph can be: Directed: Edges are directed. Undirected: Edges are undirected

1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph

1.1Data Structure and Algorithm

Introduction to Graph

 A graph consists of a set of vertices, and a set of edges that link together the vertices. 

A graph can be: Directed: Edges are directed. Undirected: Edges are undirected

Page 2: 1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph

1.2Data Structure and Algorithm

Undirected Graph

A graph G = (V, E)V: verticesE : edges, unordered pairs of vertices from V

V (u,v) is same as (v,u) Thus |E| <= |V| (|V|-1)/2

A

ONM

LKJ

E F G H

DCB

I

P

Page 3: 1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph

1.3Data Structure and Algorithm

Directed Graph

A graph G = (V, E)V: verticesE : edges, ordered pairs of vertices from

VV (u,v) is different from (v,u) Thus |E| <= |V| (|V|-1)

A

E F G H

DCB

I

Page 4: 1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph

1.4Data Structure and Algorithm

Graph in Application

Internet: web graphsEach page is a vertexEach edge represent a hyperlinkDirected

GPS: highway mapsEach city is a vertexEach freeway segment is an undirected edgeUndirected

Page 5: 1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph

1.5Data Structure and Algorithm

Representing Graphs

Using Adjacency Matrix

Using Adjacency List

Page 6: 1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph

1.6Data Structure and Algorithm

Adjacency Matrix Representation

Example: A 1 2 3 4

1 0 1 1 0

2 0 0 1 0

3 0 0 0 0

4 0 0 1 0

1

2 4

3

a

d

b c

An adjacency matrix represents the graph as a n x n matrix A:A[i, j] = 1 if edge (i, j) E (or weight of edge)

= 0 if edge (i, j) E

Space Complexity: O(V2)

Page 7: 1.1 Data Structure and Algorithm Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph

1.7Data Structure and Algorithm

Adjacency List Representation

1

2

3

4

1

2 4

3

a

d

b c

3

3

3

2

Space Complexity: O(V+E)