copyright © curt hill 2001-2006 graphs definitions and implementations

13
Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Upload: geoffrey-sims

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Copyright © Curt Hill Mathematical definitions Indegree of a node –Number of arcs ending at node Outdegree –Number of arcs starting at node Directed or digraph –Arcs are one directional only Undirected –Bidirectional graphs –Node indegree = node outdegree

TRANSCRIPT

Page 1: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Graphs

Definitions and Implementations

Page 2: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Mathematical definitions• Nodes or Vertices

– A location or point– May have a number of

properties• Arcs or Edges

– Connector of nodes – Directional or bidirectional– May also have other

properties, eg. cost

2

1

6

45

3

Page 3: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Mathematical definitions• Indegree of a node

– Number of arcs ending at node• Outdegree

– Number of arcs starting at node• Directed or digraph

– Arcs are one directional only• Undirected

– Bidirectional graphs– Node indegree = node

outdegree

2

1

6

45

3

Page 4: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Undirected graphs• Two nodes are adjacent if an arc

connects them• A path is a collection of arcs leading

from one node to another• A cycle is a path of at least three

arcs that starts and ends on the same node

• A graph is connected if there is a path from any node to any other

• A disconnected graph contains components

Page 5: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Directed graph• Each arc must start at a node and

finish at a node– Usually a different node, but self

references are possible• Each node may point at zero or more

arcs• Each node may be pointed at by zero

or more arcs• Since pointers are one directional, all

pointer based structures are digraphs

Page 6: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Directed graphs again• Also has the notion of paths and

cycles• A strongly connected digraph has a

path from every node to every other node

• A weakly connected digraph has a path from every node to every other node, only if converted to an undirected graph

Page 7: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Connectivity• A and B together are a

disconnect graph with two components

• A is strongly connected• B is weakly connected

2

1

6

45

3

8 9

7

A

B

Page 8: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Real Graphs• Which of following are directed?• Airline/bus/train routes• Highway, roads, streets• Flow graphs of programs• Circuit board component connections• Moves in games or puzzles• Molecule diagrams• People with a “works with”

relationship• People with a “works for” relationship• Rooms of a building

Page 9: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Flow Graphs

2: a = b * 2;3: while(b < 4) {4: c += a/2 + b--;5: if(a<0)6: a = -a*2 + 1;7: }8: a = 0;

2

3

4

5

6

7 8

Page 10: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Previous Data Structures• Any pointer based data structure is

graph• A straight singly linked list has indegree

one – Outdegree one for every node except last

• Circular singly linked list has all nodes with indegree=outdegree=1

• A doubly linked list increases degrees to two

• All doubly linked and circular lists contain cycles

Page 11: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Implementations• Is a pointer based structure the

obvious way to implement graphs?• The problem is the maximum

outdegree– If too large then each node has too

many pointers– Many of which may be null

• Suppose the average outdegree is 2 but the maximum is 20– How would you implement?

Page 12: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Set Representation• Set of destinations

– Each node contains a set of those nodes that are connected by an arc• 7: {8, 9}• 8: {9}

• Sets of ordered pairs– First is source, second is destination

• {(7,8),(7,9),(8,9)}

8 9

7

Page 13: Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations

Copyright © Curt Hill 2001-2006

Adjacency Matrix• Matrix contains booleans or

weights• A sparse matrix can be a list of

lists

8 9

7

7 8 97 1 28 29

1 2

2