# Graphs In Data Structure

**Topics:**Graph theory, Loop, Multigraph

**Pages:**19 (1178 words)

**Published:**April 5, 2015

Terminology

Graph ADT

Data Structures

Reading: 12.1-12.2

COSC 2011, Summer 2004

Definition

• A graph is a pair (V, E), where

– V is a set of nodes, called vertices

– E is a collection of pairs of vertices, called edges

• Both are objects (i.e. store data)

G

E

B

F

A

Vertex

city

computer

web page

airport

C

D

COSC 2011, Summer 2004

H

Edge

road

cable

hyperlink

flight

Example Applications

•

•

•

•

•

Electronic circuits

– Printed circuit board

– Integrated circuit

Transportation networks

– Highway network

– Flight network

Computer networks

– Local area network

– Internet

– Web

Databases

– Entity-relationship diagram

Questions

– Are two points connected?

– What is the shortest path between

them?

cslab1a

cslab1b

math.brown.edu

cs.brown.edu

brown.edu

qwest.net

att.net

cox.net

John

Paul

COSC 2011, Summer 2004

David

Types of edges

• Undirected edge {u,v}

– Does not indicate direction

– We can “travel” in either direction

A

B

• Directed edge (u,v)

– Has a direction

A

– We can only “travel” in one direction along the edge

B

• Self-loop

– Edge that originates and ends at the same vertex

• Parallel edges

A

– Two undirected edges with same end vertices

– Two directed edges with same origin and destination

COSC 2011, Summer 2004

A

B

Types of graphs

• Undirected graph

– All edges are undirected

• Directed graph

– All edges are directed

• Mixed graph

– Has both directed and undirected edges

• Simple graph

– No parallel edges and self-loops

COSC 2011, Summer 2004

Example

find route from C to G

G

E

B

F

A

C

D

H

G

E

B

F

A

C

D

COSC 2011, Summer 2004

H

Vertices and Edges

•

•

•

•

•

•

End vertices (or endpoints) of an edge

– U and V are the endpoints of a

Edges incident on a vertex - all edges that have the vertex as endpoint – a, d, and b are incident on V

Adjacent vertices - endpoints of the same edge

– U and V are adjacent

Degree of a vertex - number of edges incident

V

h

b

a

– X has degree 5

Parallel edges

U

X

d

– h and i are parallel edges

Self-loop

c

e

i

– j is a self-loop

W

g

f

COSC 2011, Summer 2004

Y

j

Z

Vertices - directed graphs

•

•

•

•

End points

– Origin - first endpoint

– Destination - second endpoint

Outgoing edges of a vertex - directed edges that originate at the vertex – a and d originate in V

Incoming edges of a vertex - directed edges that terminate at the vertex – a and c terminate at U

V

Degree of a vertex

b

a

– Number of incoming and outgoing edges

U

X

d

c

e

W

COSC 2011, Summer 2004

h

g

f

Y

Z

i

Graphs - Basic Properties

Notation

n

m

deg(v)

number of vertices

number of edges

degree of vertex v

A

Property 1

B

Σv deg(v) = 2m

Proof: each edge is counted twice

Property 2

C

In a directed graph

Σv indeg(v) = Σv outdeg(v) = m

Proof: each edge contributes once

to outdeg sum of its origin, once

to the indeg of its destination

COSC 2011, Summer 2004

Example

– n=4

D – m=5

–

–

–

–

deg(A)=2

deg(C)=2

deg(B)=3

deg(D)=3

– Σv deg(v) = 10

Graphs - Basic Properties

Property 3

Notation

In a simple undirected graph (no

self-loops and no parallel edges)

m ≤ n (n − 1)/2

Proof: each vertex has degree at

most (n − 1)

What is the bound for a directed graph?

COSC 2011, Summer 2004

n

m

deg(v)

number of vertices

number of edges

degree of vertex v

Example

– n=4

– m=6

– deg(v) = 3

Graph ADT - Main Methods

•

Vertices and edges

•

– are positions

– store elements

•

– insertVertex(o): insert a vertex

storing element o

– insertEdge(v, w, o): insert an

edge (v,w) storing element o

– removeVertex(v): remove vertex

v (and its incident edges)

– removeEdge(e): remove edge e

Accessor methods

– endVertices(e): an...

Please join StudyMode to read the full document