24
1 Graphs (1) Graphs (1) Outline and Required Reading: Outline and Required Reading: COSC 2011, Fall 2003, Section A Instructor: N. Vlajic The Graph ADT (§ 12.1) Data Structures for Graphs (§ 12.2)

Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

1

Graphs (1)Graphs (1)

Outline and Required Reading:Outline and Required Reading:

COSC 2011, Fall 2003, Section AInstructor: N. Vlajic

• The Graph ADT (§ 12.1)• Data Structures for Graphs (§ 12.2)

Page 2: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

2

Graph Graph – most general data structure - consists of set of vertices (V)and a set of edges (E) that connect vertices – G = {V, E}

Graphs

• vertices can also be referred to as nodes

• in Graph ADT, both vertices and edges are Positions, i.e theystore elements (e.g. v.element(), e.element())

Application Application – road maps, computer networks, WWW, activity charts(Are two points connected? What is the shortest path between them? etc. )

AB E

FDC

vertex =city

computerWeb-page

task

edge =roadcable

hyperlinktransition state

Page 3: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

3

Graphs: Types of Edges

Edge Edge – connects two vertices (u and v), can be undirected or directed(1) undirected edge {u,v} does not indicated direction – we can

can “travel” in either direction along the edge(2) directed edge (u,v) has a direction – we can “travel” only in

one direction along the edge

A B DC E

SelfSelf--LoopLoop – edge (undirected or directed) that originates and ends at thesame vertex

Parallel EdgesParallel Edges – two undirected edges with the same end vertices, ortwo directed edges with the same origin and destination

GFundirected edge directed edge self-edge parallel edges

Page 4: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

4

Graphs: Types of Graphs

Undirected GraphUndirected Graph – graph whose edges are all undirected

Directed GraphDirected Graph – graph whose edges are all directed

• undirected and mixed graphs can be converted into adirected graph by replacing each undirected edge {u,v}by the pair of directed edges (u,v) and (v,u)

Mixed GraphMixed Graph – graph that has both directed and undirected edges

A

BE

F

DC

A

BE

F

DC

How could vertex F be reached from vertex C in the two graphs?

Simple GraphSimple Graph – graph without parallel edges and self-loops

Page 5: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

5

Graphs: Types of Vertices

End Vertices (Endpoints) of an EdgeEnd Vertices (Endpoints) of an Edge – two vertices joined by the edge

Adjacent VerticesAdjacent Vertices – endpoints of the same edge

• in a directed edge, the first endpointis origin and the other is destination

A

BE

F

DC

mn

op

qr

s

Edge Incident on VertexEdge Incident on Vertex – vertex is one of the edge’s endpoints

Outgoing Edges of a VertexOutgoing Edges of a Vertex – directed edges that originate at the vertex

Incoming Edges of a VertexIncoming Edges of a Vertex – directed edges that terminated at the vertex

Degree of a VertexDegree of a Vertex – deg(v) = number of incoming and outgoing edges of v

Page 6: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

6

Graph: Basic Properties

Property 1:Property 1: If G is a graph with m edges then 2mdeg(v)Gv

=∑∈

A

BE

F

DC

mn

op

qr

s

ProofProof On the left-hand side, every undirected edge (u,v) is counted twice – once by its endpoint u and once by its endpoint v.

Property 2:Property 2: If G is a directed graph, then

ProofProof Each directed edge (u,v) contributes once to indeg sum of its origin, andonce to outdeg sum of its destination

moutdeg(v)indeg(v)GvGv

== ∑∑∈∈

A

BE

F

DC

mn

op

qr

s

Page 7: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

7

Graphs: Basic Properties (cont.)

Property 3:Property 3: In an undirected graph with n vertices and m edges (no self-loops and parallel edges) the following must hold

21)n(nm −≤

ProofProof Assume all n vertexes are mutually connected (case with max # of edges).

A

DC

B

E

21)-n(n 1 ... 2)-(n 1)-(nm =+++=

)deg(v..)deg(v)deg(v)deg(vm },..,v,v{v-G inn

},v{v-G in3

}{v-G in2

G in1 1-n21211 ++++=

A

DC

B

E A

DC

B

E

Page 8: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

8

Graphs: Paths and Cycles

Path Path – sequence of alternating vertices and edges – starts at a vertex and ends at a vertex

Simple Path Simple Path – path such that all its vertices and edges are distinct

Cycle Cycle – path with identical start and end vertex

Simple Cycle Simple Cycle – cycle such that all its vertices and edges are distinct

A

BE

F

C D

mn

qo

r

ps

tA

BE

F

C D

mn

qo

r

ps

t

Path = (F, s, D, t, E, p, F, n, B) Simple Path = (C, q, A, o, E)

Cycle = (E, p, F, n, B, m, A, o, E, t, D, s, F, p, E) Simple Cycle = (E, t, D, r, C, q, A, o, E)

Page 9: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

9

Graphs: Subgraphs

Subgraph Subgraph – graph in G whose vertices and edges are subset of verticesand edges of G

BE

F

C D

m

qr

Subgraph ={A, B, C, D} U {m, q, r}

BE

F

C D

m

r

ps

t

Spanning Spanning Subgraph Subgraph – subgraph in G that contains all the vertices of G

Spanning Subgraph = = {A, B, C, D, E, F} U {m, o, p, s, t, r}

Connected Graph Connected Graph – for any two vertices there is a path between them

Connected Components of a Graph Connected Components of a Graph – maximal connected subgraphs

A A

BE

Fmn

o

r

pA

Graph with 2 Connected Components

C D

Page 10: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

10

Graphs: Forests and Trees

Forest Forest – undirected graph without cycles

Free Tree Free Tree – connected forest (free = has no root)

Spanning Tree Spanning Tree – subgraph of G that contains all of G’s vertices and enough of its edges to form a tree

• connected components of a forest are free trees

BE

C D

m

q

soAF

Forest

BE

m

q

soAF

p

Spanning Tree

• in a graph with cycles there is more than one spanning tree

C D

Page 11: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

11

Graphs: Forests and Trees

Example 1 [ spanning trees ]

BE

AF

C D

B

AF

C D

EB

AF

C D

E

B

AF

C D

EB F

D

EA

B F

D

E

C

A

C

Spanning Tree 1 Spanning Tree 2

Spanning Tree 3 Spanning Tree 4 Spanning Tree 5

Are there any other spanning threes besides these? Find them!

Page 12: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

12

Graphs: Forests and Trees (cont.)

Property 4:Property 4: Let G be an undirected graph with n vertices and m edges

(1) If G is a (spanning) tree

(2) If G is a forest

(3) If G is connected

1nm −=

1nm −≥

1nm −≤

BE

C DA

FE

C

AFB

D

Page 13: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

13

Graph ADT: Interface

General MethodsGeneral Methods public int numVertices();/* return the number of vertices in G */

public int numEdges();/* return the number of edges in G */

public Iterator vertices();/* return an iterator of the vertices of G */

public Iterator edges();/* return an iterator of the edges of G */

public Vertex aVertex();/* return an arbitrary vertex of G */

public int degree(Vertex v);/* return the degree of v */

public Iterator adjacentVertices(Vertex v);/* return an iterator of the vertices adjacent to v */

Usual MethodsUsual Methods size(), isEmpty(), elements(), positions(),replaceElement(p,o), swapElements(p,q)

Page 14: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

14

public Iterator incidentEdges(Vertex v);/* return an iterator of the edges incident upon v */public Vertex[ ] endVertices(Edge e);/* return a 2-D array storing the end vertices of e */

public Vertex opposite(Vertex v, Edge e);/* return the endpoint of edge e distinct from v */

public boolean areAdjacent(Vertex v, Vertex w);/* return whether vertices v and w are adjacent */

Graph ADT: Interface (cont.)

public Vertex insertVertex(Object o);/* insert and return a new (isolated) vertex storing the

object o at this position */

public Edge insertEdge(Vertex v, Vertex w, Object o);/* insert and return an undirected edge between vertices

v and w, storing the object o at this position */

public void removeVertex(Vertex v);/* remove vertex v and all its incident edges */

public void removeEdge(Edge e);/* remove edge e */

Update MethodsUpdate Methods

Page 15: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

15

Data Structures for Graphs

Graph Implementation TechniquesGraph Implementation Techniques

1. Edge List StructureEdge List Structure

2. Adjacency List StructureAdjacency List Structure

3. Adjacency MatrixAdjacency Matrix

• in all three techniques, all vertices are stored in a container

• in 1 and 2, only “existing” edges are stored in a container(space occupancy: O(n+m))

• in 3, a place holder for an edge between each pair of vertices is provided (space occupancy: O(n2)

Page 16: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

16

1. Edge List Structure

Vertex Object Vertex Object – contains references to• element• corresponding position in vertex sequence

Edge Object Edge Object – contains references to• element• corresponding position in edge sequence• origin vertex object• destination vertex object

A

CB

DA B C D

o p

q

r

o p q r

E

V

• counter for the number of (un)directed edges

Page 17: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

17

1. Edge List Structure (cont.)

Edge List Structure Edge List Structure – the simples implementation of a graph• each vertex / edge is represented by an object

• all vertices are stored in container V, all edges are stored in container E

• direct access from edges to the vertices they areincident upon is provided – endVertices, origin,destination, opposite run in O(1) time

• access to edges incident upon a given vertex isinefficient – all edge objects must be inspected

A

CB

DA B C D

o p

q

r

o p q r

E

V

Page 18: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

18

1. Edge List Structure (cont.)

Graph Method

size, isEmpty, replaceElement, swapElementnumVertices, numEdges, aVertex

degree, inDegree, outDegreeelements, positions

endVertices, origin, destination, oppositeincidentEdges, adjacentVertices, areAdjacent

removeVertex…

Time

O(1)O(1)O(1)

O(n+m)O(1)O(m)O(m)

Running Times

each vertexstores a

counter forthese values

Space Requirements – O(m+n), where m = # of edges, n = # of vertices

Page 19: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

19

2. Adjacency List Structure

Vertex Object Vertex Object – as in Edge List Structure, plus

• reference to incidence container I(v) that stores referencesto all edges incident on v

Edge Object Edge Object – as in Edge List Structure, plus

• reference to the corresponding positions of the edge in theincident containers I(u) and I(v)

Adjacency List Structure Adjacency List Structure – extends Edge List Structure to support direct access to the incident edges (andadjacent vertices)• vertex-based methods run in O(1) time

Page 20: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

20

2. Adjacency List Structure (cont.)

A

CB

B A C

o p

o p

E

V

I(B) I(A) I(C)

What is the cost and what steps are involved in executing adjacentVertices(A)?

What is the cost and what steps are involved in executing incidentEdges(A)?

What is the cost and what steps are involved in executing areAdjacent(A,B)?

Page 21: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

21

Graph Method

size, isEmpty, replaceElement, swapElementnumVertices, numEdges, aVertex

degree, inDegree, outDegreeelements, positions

endVertices, origin, destination, oppositeincidentEdges, adjacentVertices,

areAdjacentremoveVertex

Time

O(1)O(1)O(1)

O(n+m)O(1)

O(deg(v))O(min(deg(v1),deg(v2)))

O(deg(v))

Running Times

2. Adjacency List Structure (cont.)

Space Requirements – O(m+n) + ∑∑∑∑deg(v) = O(m+n)

Page 22: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

22

3. Adjacency Matrix Structure

Vertex Object Vertex Object – as in Edge List Structure, plus• distinct integer in the range 0, 1, .., n-1, called “index of v”

Edge Object Edge Object – as in Edge List Structure

• A(i,j) = reference to edge (i,j), if such edge exists;• otherwise A(i,j) = null

Adjacency Matrix Structure Adjacency Matrix Structure – extends Edge List Structure and enables that adjacency between a pair of verticesbe determined in constant time• cost: O(n2) space usage

Adjacency Matrix Adjacency Matrix – an n x n array (A)

Page 23: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

23

3. Adjacency Matrix Structure (cont.)

A

CBo p

B A C

V

o p

E

0 1 2

0 1 2012

∅∅

∅0

1

2 ∅

What is the cost and what steps are involved in executing areAdjacent(A,B)?

What is the cost and what steps are involved in executing incidentEdges(A)?

What is the cost and what steps are involved in executing adjacentVertices(A)?

Page 24: Graphs (1) - site.iugaza.edu.pssite.iugaza.edu.ps/.../uploads/DatStr_161_Graphs.pdf · 2 Graph – most general data structure - consists of set of vertices (V) and a set of edges

24

3. Adjacency Matrix Structure (cont.)

Graph Method

size, isEmpty, replaceElement, swapElementnumVertices, numEdges, aVertex

degree, inDegree, outDegreeelements, positions

endVertices, origin, destination, oppositeincidentEdges, adjacentVertices,

areAdjacentinsertVertex, removeVertex

Time

O(1)O(1)O(1)

O(n+m)O(1)O(n)O(1)O(n2)

Running Times

Space Requirements – O(m+n) + O(n2) = O(n2)

a whole newarray of

smaller/largersize must be

created