lecture 9:

12
Lecture 9: Graphs & Graph Models

Upload: garren

Post on 20-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Lecture 9:. Graphs & Graph Models. Definition of a Graph. cycle. path. edge. vertex. A. D. F. C. H. B. E. G. Graph Representations. edge list. node list. A B A C A D A E A F B A B C B E B H C A C B C D C E C H D A D C D F D G D H. A - B C D E F B - A C E H - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 9:

Lecture 9:

Graphs & Graph Models

Page 2: Lecture 9:

Definition of a Graph

edge

vertex

cycle

path

Page 3: Lecture 9:

Graph Representations

adjacency matrix

node list edge listA D F

C H

B E G

A B C D E F G HA - 1 1 1 1 1 0 0B 1 - 1 0 1 0 0 1C 1 1 - 1 1 0 0 1D 1 0 1 - 0 1 1 1E 1 1 1 0 - 1 1 0F 1 0 0 1 1 - 1 1G 0 0 0 1 1 1 - 1H 0 1 1 1 0 1 1 -

A - B C D E FB - A C E HC - A B D E HD - A C F G HE - A B C F GF - A D E G HG - D E F HH - B C D F G

A BA CA DA EA FB AB CB EB HC AC BC DC EC HD AD CD FD GD H

E AE BE CE FE GF AF DF EF GF HG DG EG FG HH BH CH DH FH G

node list - lists the nodes connected to each node

edge list - lists each of the edges as a pair of nodes undirected edges may be listed twice XY and YX in order to simplify algorithm implementation

adjacency matrix - for an n-node graph we build an nxn array with 1's indicating edges and 0's no edge the main diagonal of the matrix is unused unless a node has an edge connected to itself. If graph is weighted, 1's are replaced with edge weight values

Page 4: Lecture 9:

Example Applications

This graph could represent...

a computer network

an airline flight route

an electrical power grid

WalMart warehouse supply lines

...

Page 5: Lecture 9:

Niche Overlap Graph

Animals are represented as nodes. We place an edge between two nodes if the corresponding animals compete for resources (food, habitat, ...).

Page 6: Lecture 9:

Web Links

Vertices indicate we pages and arcs indicated links to other web pages.

Page 7: Lecture 9:

Round-Robin Tour

undefeated 5-0

biggest loser 0-5

A directed graph represents a round-robin tour in which each team plays every other team exactly one time. Arc points from winning team to losing team.

Page 8: Lecture 9:

An Acquaintanceship Graph

EduardoKamini

JanPaulaTodd

KamleshLilaAmy

ChingLiz

SteveJoelGailKokoKari

Shaquira

Eduardo

Kamini

Jan

Paula

Todd

Kamlesh

Lila

Amy

Ching

Liz

Steve

Joel

Gail

Koko

Kari

Shaquira

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 00 0 1 0 0 0 0 0 0 0 0 0 0 0 0 00 1 0 1 0 0 1 0 0 0 0 1 0 0 0 01 0 1 0 1 0 1 1 0 1 0 1 0 0 0 00 0 0 1 0 1 0 1 0 0 1 0 0 0 0 00 0 0 0 1 0 0 0 1 0 0 0 0 0 0 00 0 1 1 0 0 0 0 0 1 0 1 1 0 0 00 0 0 1 1 0 0 0 0 1 1 0 0 0 0 00 0 0 0 0 1 0 0 0 0 0 0 0 0 0 00 0 0 1 0 0 1 1 0 0 1 0 0 0 0 00 0 0 0 1 0 0 1 0 1 0 0 0 1 0 10 0 1 1 0 0 1 0 0 0 0 0 1 0 1 00 0 0 0 0 0 1 0 0 0 0 1 0 0 0 10 0 0 0 0 0 0 0 0 0 1 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 1 0 0 0 00 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0

0 3 2 1 2 3 2 2 4 2 3 2 3 4 3 43 0 1 2 3 4 2 3 5 3 4 2 3 5 3 42 1 0 1 2 3 1 2 4 2 3 1 2 4 2 31 2 1 0 1 2 1 1 3 1 2 1 2 3 2 32 3 2 1 0 1 2 1 2 2 1 2 3 2 3 23 4 3 2 1 0 3 2 1 3 2 3 4 3 4 32 2 1 1 2 3 0 2 4 1 2 1 1 3 2 22 3 2 1 1 2 2 0 3 1 1 2 3 2 3 24 5 4 3 2 1 4 3 0 4 3 4 5 4 5 42 3 2 1 2 3 1 1 4 0 1 2 2 2 3 23 4 3 2 1 2 2 1 3 1 0 3 2 1 4 12 2 1 1 2 3 1 2 4 2 3 0 1 4 1 23 3 2 2 3 4 1 3 5 2 2 1 0 3 2 14 5 4 3 2 3 3 2 4 2 1 4 3 0 5 23 3 2 2 3 4 2 3 5 3 4 1 2 5 0 34 4 3 3 2 3 2 2 4 2 1 2 1 2 3 0

static void floyd(){ for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if(i!=j) W[i, j]=min(W[i,j],W[i,k],W[k,j]); } } }}

In an acquaintanceship graph people are represented as nodes. Nodes are connected by an edge when two people know each other. We can compute the degree of separation between any two people using Floyd's All-Pairs Shortest-Path Algorithm.

Page 9: Lecture 9:

Simple Graphs vs Multigraphs

A simple graph is one in which each edge connects two different vertices and where on two edges connect the same pair of vertices.

Graphs that have multiple edges connecting the same pairs of vertices are called multigraphs or pseudographs.

An edge that connects a node to itself is called a loop.

A D F

C H

B E G

Page 10: Lecture 9:

ring complete graph bipartite graph

directed acyclic graph

Types of Graphs

Page 11: Lecture 9:

A D F

C H

B E G

Graph Breadth-First Traversal

Given a graph G(V,E) and a starting vertex s, performa breadth-first traversal (BFT) of G. such that eachreachable vertex is entered exactly once.

If all vertices are reachable, the edges traversed andthe set of vertices will represent a spanning treeembedded in the graph G.

1) BFT suggests an iterative process (rather than a recursive one)

2) BFT vertices order of traversal can be maintained using a Queue data structure

3) The preferred representation for the graph is an adjacency matrix

4) We will need a way to keep up with which vertices have been "used" (e.g. a Boolean list)

5) Process begins by placing the starting vertex in the Queue

6) A vertex is taken from the Queue, every unused vertex adj to this vertex is added to the Queue This operation is repeated until the Queue is empty.

8) The output (answer) is returned in the form of a list of vertices in the order they entered the Queue

Page 12: Lecture 9:

Graph Depth-First Traversal

A D F

C H

B E G

Given a graph G(V,E) and a starting vertex s, performa depth-first traversal (BFT) of G. such that eachreachable vertex is entered exactly once.

If all vertices are reachable, the edges traversed andthe set of vertices will represent a spanning treeembedded in the graph G.

1) DFT suggests a recursive process (rather than an iterative one)

2) DFT vertices order of traversal are maintained automatically by the recursion process (as a Stack)

3) The preferred representation for the graph is an adjacency matrix.

4) We will need a way to keep up with which vertices have been "used" (e.g. a Boolean list)

5) Process begins by passing the starting vertex to a recursive function DFT(s)

6) For the current vertex, s DFT(s) calls itself for each adjacent, unused vertex remaining. This operation is completed when all calls to DFT( ) are completed.

8) The output is returned as a of a list of vertices in the order they were passed to DFT( ).