Graphs

26
Graphs Graphs All tree structures are hierarchical. This All tree structures are hierarchical. This means that each node can only have one parent means that each node can only have one parent node. Trees can be used to store data which node. Trees can be used to store data which has a definite hierarchy; for example a has a definite hierarchy; for example a family tree or a computer file system. family tree or a computer file system. Some data need to have connections between Some data need to have connections between items which do not fit into a hierarchy like items which do not fit into a hierarchy like this. Graph data structures can be useful in this. Graph data structures can be useful in these situations. these situations. A graph consists of a number of data items, A graph consists of a number of data items, each of which is called a vertex. Any vertex each of which is called a vertex. Any vertex may be connected to any other, and these may be connected to any other, and these connections are called edges. connections are called edges.

description

Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite hierarchy; for example a family tree or a computer file system. - PowerPoint PPT Presentation

Transcript of Graphs

Page 1: Graphs

GraphsGraphs

All tree structures are hierarchical. This means that each node All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data can only have one parent node. Trees can be used to store data which has a definite hierarchy; for example a family tree or a which has a definite hierarchy; for example a family tree or a computer file system.computer file system.

Some data need to have connections between items which do Some data need to have connections between items which do not fit into a hierarchy like this. Graph data structures can be not fit into a hierarchy like this. Graph data structures can be useful in these situations. useful in these situations.

A graph consists of a number of data items, each of which is A graph consists of a number of data items, each of which is called a vertex. Any vertex may be connected to any other, called a vertex. Any vertex may be connected to any other, and these connections are called edges. and these connections are called edges.

Page 2: Graphs

Extremely useful tool in modelling problemsExtremely useful tool in modelling problems Consist of:Consist of:

VerticesVertices EdgesEdges

GraphsGraphs

DE

AC

FB

VertexEdge

Vertices can beconsidered “sites”or locations.

Edges representconnections.

Page 3: Graphs

ApplicationsApplicationsAir flight system

• Each vertex represents a city• Each edge represents a direct flight between two cities• A query on direct flights becomes a query on whether an edge exists• A query on how to get to a location is “does a path exist from A to B”• We can even associate costs to edges (weighted graphs), then ask “what is the cheapest path from A to B”

Page 4: Graphs

Wireless communicationWireless communication

Another applicationAnother application

• Can be represented by a weighted complete graph (every two vertices are connected by an edge). • Each edge represents the Euclidean distance dij between two stations. • Each station uses a certain power i to transmit messages. Given this power i, only a few nodes can be reached (bold edges). A station reachable by i then use its own power to relay the message to other stations not reachable by i.• A typical wireless communication problem is: how to broadcast between all stations such that they are all connected and the power consumption is minimized.

Page 5: Graphs

Some other applications:Some other applications:

Electronic circuits:Electronic circuits:

To find the least resistance between two nodes.To find the least resistance between two nodes. Computer networks:Computer networks:

To find the smallest path between 2 computers.To find the smallest path between 2 computers. Databases:Databases:

To draw the entity relationship(ER) diagram. To draw the entity relationship(ER) diagram.

Page 6: Graphs

Undirected graphUndirected graph An undirected graph is specified by an ordered pair (V,E), where V is An undirected graph is specified by an ordered pair (V,E), where V is

the set of vertices and E is the set of edgesthe set of vertices and E is the set of edges

DefinitionDefinition

{c,f}

{a,c}{a,b}

{b,d} {c,d}

{e,f}

{b,e}

Page 7: Graphs

If If vv11 and and vv22 are connected, they are said to be adjacent are connected, they are said to be adjacent

verticesvertices vv11 and and vv22 are are endpointsendpoints of the edge { of the edge {vv11, , vv22}}

If an edge If an edge ee is connected to is connected to vv, then , then vv is said to be incident is said to be incident on on ee. Also, the edge . Also, the edge ee is said to be incident on is said to be incident on vv..

{{vv11, , vv22} = {} = {vv22, , vv11}*}*

TerminologyTerminology

*Later, we will talk about “directed graphs”, where edges have direction. Thismeans that {v1,v2} ≠ {v2,v1} . Directed graphs are drawn with arrows (called arcs) between edges. A B This means {A,B} only, not {B,A}

Page 8: Graphs

Two popular computer representations of a graph. Both represent Two popular computer representations of a graph. Both represent the vertex set and the edge set, but in different ways.the vertex set and the edge set, but in different ways.

1.1. Adjacency MatrixAdjacency Matrix

Use a 2D matrix to represent the graphUse a 2D matrix to represent the graph

1.1. Adjacency ListAdjacency List

Use a 1D array of linked listsUse a 1D array of linked lists

Graph RepresentationGraph Representation

Page 9: Graphs

Adjacency MatrixAdjacency Matrix

• 2D array A[0..n-1, 0..n-1], where n is the number of vertices in the graph• Each row and column is indexed by the vertex id.

- e,g a=0, b=1, c=2, d=3, e=4• An array entry A [i] [j] is equal to 1 if there is an edge connecting vertices i and j. Otherwise, A [i] [j] is 0.• The storage requirement is Θ(n2). Not efficient if the graph has few edges.• We can detect in O(1) time whether two vertices are connected.

Page 10: Graphs

Adjacency listAdjacency list

• The adjacency list is an array A[0..n-1] of lists, where n is the number of vertices in the graph. Each array entry is indexed by the vertex id (as with adjacency matrix)• The list A[i] stores the ids of the vertices adjacent to i.

Page 11: Graphs

ExamplesExamples

2

4

3

5

1

76

9

8

0

01000000109

10000001018

00010000107

00101000006

00010010005

00000011004

00001100103

01000100102

10100011001

01000000000

9876543210

Page 12: Graphs

ExamplesExamples

2

4

3

5

1

76

9

8

0

9

8

7

6

5

4

3

2

1

0

9732

8

841

541

32

63

75

61

920

81

Page 13: Graphs

Adjacency ListsAdjacency Lists More compact than adjacency matrices if graph has few edgesMore compact than adjacency matrices if graph has few edges Requires more time to find if an edge existsRequires more time to find if an edge exists

Adjacency MatrixAdjacency Matrix Always require nAlways require n22 space space

This can waste a lot of space if the number of edges are sparseThis can waste a lot of space if the number of edges are sparse Can quickly find if an edge existsCan quickly find if an edge exists

Adjacency Lists vs. MatrixAdjacency Lists vs. Matrix

Page 14: Graphs

A path is a sequence of vertices (vA path is a sequence of vertices (v00, v, v11, v, v22,… v,… vkk) such that:) such that:

For For 0 ≤ i < k, {v0 ≤ i < k, {vii, v, vi+1i+1}} is an edge is an edge

For For 0 ≤ i < k-1, v0 ≤ i < k-1, vi i ≠ v≠ vi+2 i+2

That is, the edge {vThat is, the edge {vii, v, vi+1i+1} ≠ {v} ≠ {vi+1i+1, v, vi+2i+2}}

Note: a path is allowed to go through the same vertex or the same edge any Note: a path is allowed to go through the same vertex or the same edge any number of times!number of times!

The length of a path is the number of edges on the pathThe length of a path is the number of edges on the path

Path between verticesPath between vertices

Page 15: Graphs

A path is simple if and only if it does not contain a vertex more than A path is simple if and only if it does not contain a vertex more than once.once.

A path is a cycle if and only if vA path is a cycle if and only if v00= v= vkk

The beginning and end are the same vertex!The beginning and end are the same vertex!

A path contains a cycle if some vertex appears twice or moreA path contains a cycle if some vertex appears twice or more

Types of pathsTypes of paths

Page 16: Graphs

ExamplesExamples

1. {a,c,f,e}

1. {a,b,d,c,f,e}

1. {a, c, d, b, d, c, f, e}

2. {a,c,d,b,a}

1. {a,c,f,e,b,d,c,a}

Are these paths?

Any cycles?

What is the path’s length?

Page 17: Graphs

A graph is directed if direction is assigned to each A graph is directed if direction is assigned to each edge. We call the directed edges edge. We call the directed edges arcsarcs.. An edge is denoted as an ordered pair (u, v) An edge is denoted as an ordered pair (u, v)

Recall: for an undirected graphRecall: for an undirected graph An edge is denoted {u,v}, which actually corresponds An edge is denoted {u,v}, which actually corresponds

to two arcs (u,v) and (v,u) to two arcs (u,v) and (v,u)

Directed GraphDirected Graph

Page 18: Graphs

A path in a directed graph must follow the direction of A path in a directed graph must follow the direction of the arrows.the arrows.

A directed graph is connected if, for any pair of A directed graph is connected if, for any pair of vertices, there is a path between them.vertices, there is a path between them.

Is the above graph connected? Is the above graph connected?

D

EA

C

Page 19: Graphs

RepresentationsRepresentations

Page 20: Graphs

A directed path is a sequence of vertices A directed path is a sequence of vertices (v(v00, v, v11, . . . , v, . . . , vkk)) Such that (vSuch that (vii, v, vi+1i+1) is an ) is an arcarc

A directed cycle A directed cycle is a directed path such that is a directed path such that the first and last vertices are the same.the first and last vertices are the same.

A directed graph is A directed graph is acyclicacyclic if it does not if it does not contain any directed cyclescontain any directed cycles

Directed Acyclic GraphDirected Acyclic Graph

Page 21: Graphs

ExampleExample

0

12

3

45

6

7

8

9

Indeg(2)?

Indeg(8)?

Outdeg(0)?

Num of Edges?

Total OutDeg?

Total Indeg?

Page 22: Graphs

Directed graphs are often used to represent order-dependent Directed graphs are often used to represent order-dependent taskstasks

That is we cannot start a task before another task finishesThat is we cannot start a task before another task finishes We can model this task dependent constraint using We can model this task dependent constraint using arcsarcs An An arc (i,j) arc (i,j) means means task jtask j cannot start until cannot start until task itask i is finished is finished

Clearly, for the system not to hang, the graph must be acyclic. Clearly, for the system not to hang, the graph must be acyclic.

Directed Graphs Directed Graphs UsageUsage

i j

Task j cannot start until task i is finished

Page 23: Graphs

Self loops: An edge from an vertex to Self loops: An edge from an vertex to itself is called self loop.itself is called self loop.

A complete graph is a graph which has A complete graph is a graph which has maximum number of edges.maximum number of edges.

For undirected graph with n vertices it is For undirected graph with n vertices it is n(n-1)/2.n(n-1)/2.

For directed graph it is n(n-1).For directed graph it is n(n-1). Subgraph of G(V,E): Is a graph G’ Subgraph of G(V,E): Is a graph G’

=(v’,e’) where v’is subset of V and e’ is =(v’,e’) where v’is subset of V and e’ is subset of E. subset of E.

Other DefinitionsOther Definitions

Page 24: Graphs

Path: A path from vertex vi to vj is a Path: A path from vertex vi to vj is a sequence of vertices vi, vi1,vi2……vj such sequence of vertices vi, vi1,vi2……vj such that <vi,vi1><vi1,vi2>…..<vin,vj> edges in that <vi,vi1><vi1,vi2>…..<vin,vj> edges in the graph.the graph.

A simple path is a path in which all vertices A simple path is a path in which all vertices except possibly the start and end are distinct.except possibly the start and end are distinct.

A cycle is a simple path in which start and A cycle is a simple path in which start and end vertex are same.end vertex are same.

In an undirected graph two vertices vi and vj In an undirected graph two vertices vi and vj are said to be cnnected if there exists a path are said to be cnnected if there exists a path in G from vi to vj.in G from vi to vj.

Page 25: Graphs

Connected graph: An undirected graph is Connected graph: An undirected graph is called connected if for every pair of called connected if for every pair of vertices vi and vj there exists a path from vertices vi and vj there exists a path from vi to vj.vi to vj.

A connected component of an undirected A connected component of an undirected graph is the maximal connected subgraph.graph is the maximal connected subgraph.

Tree is an acyclic graphTree is an acyclic graph Strongly connected graph:a directed Strongly connected graph:a directed

graph is said to be strongly connected if graph is said to be strongly connected if for every pair of vertices vi and vj in V(G) for every pair of vertices vi and vj in V(G) there is a directed path between vi and vj there is a directed path between vi and vj and also from vj and vi.and also from vj and vi.

Page 26: Graphs

Strongly connected component:Strongly connected component: Is a maximal subgraph that is strongly Is a maximal subgraph that is strongly

connected.connected. Degree: degree of a vertex is number of Degree: degree of a vertex is number of

edges incident on that vertex.edges incident on that vertex. Indegree of v:Number of edges that has Indegree of v:Number of edges that has

v as head in the directed graph.v as head in the directed graph. Outdegree of v: Number of edges that Outdegree of v: Number of edges that

has v as tail.has v as tail.