Copyright © Curt Hill Graphs Definitions and Implementations
Copyright © Curt Hill 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
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
Copyright © Curt Hill 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
Copyright © Curt Hill 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
Copyright © Curt Hill 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
Copyright © Curt Hill Connectivity A and B together are a disconnect graph with two components A is strongly connected B is weakly connected A B
Copyright © Curt Hill 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
Copyright © Curt Hill 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;
Copyright © Curt Hill 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
Copyright © Curt Hill 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?
Copyright © Curt Hill 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
Copyright © Curt Hill Adjacency Matrix Matrix contains booleans or weights A sparse matrix can be a list of lists