Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations."— Presentation transcript:

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

2 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 4 5 3

3 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 4 5 3

4 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

5 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

6 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

7 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 4 5 3 8 9 7 A B

8 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

9 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 78

10 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

11 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?

12 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

13 Copyright © Curt Hill 2001-2006 Adjacency Matrix Matrix contains booleans or weights A sparse matrix can be a list of lists 8 9 7 789 712 82 9 1 2 2


Download ppt "Copyright © Curt Hill 2001-2006 Graphs Definitions and Implementations."

Similar presentations


Ads by Google