cs 201 computer systems programming chapter 14 “ heap space and graphs ”

16
1 CS 201 Computer Systems Programming Chapter 14 “Heap Space and Graphs” Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS Status 11/7/2012 Status 11/7/2012

Upload: venice

Post on 19-Mar-2016

31 views

Category:

Documents


3 download

DESCRIPTION

Herbert G. Mayer, PSU CS Status 11/7/2012. CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”. Syllabus. Goal Heap Space Definition of Graph Building a Graph Graph Data Structure References. SCC not covered here. Goal. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

1

CS 201Computer Systems Programming

Chapter 14“Heap Space and Graphs”

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSStatus 11/7/2012Status 11/7/2012

Page 2: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

2

Syllabus GoalGoal Heap SpaceHeap Space Definition of GraphDefinition of Graph Building a GraphBuilding a Graph Graph Data StructureGraph Data Structure ReferencesReferences

Page 3: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

3

GoalImprove students’ understanding and use of dynamic memory Improve students’ understanding and use of dynamic memory

management: using the system heap to management: using the system heap to allocate allocate and and freefree data data spacespace

Sometimes heap usage is explained by using and exercising linked Sometimes heap usage is explained by using and exercising linked lists that can grow and shrink dynamicallylists that can grow and shrink dynamically

For a system programmer that would be too simple; so we shall For a system programmer that would be too simple; so we shall exercise heap management by building directed, cyclic graphsexercise heap management by building directed, cyclic graphs

Our graphs may be unconnected and each vertex may have any Our graphs may be unconnected and each vertex may have any number of successor nodesnumber of successor nodes

Page 4: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

4

Heap SpaceStatic data space Static data space of a program exists during whole execution; of a program exists during whole execution;

created at program load time, returned at program exit() time.created at program load time, returned at program exit() time.

Automatic spaceAutomatic space is data space created by the run-time system. is data space created by the run-time system. Each time a new procedural (or function) scope is entered, Each time a new procedural (or function) scope is entered, automatic space is allocated off the stack (could also be from automatic space is allocated off the stack (could also be from the heap). Part of the return operation is to free such previously the heap). Part of the return operation is to free such previously granted automatic space.granted automatic space.

Controlled space Controlled space is data space allocated by a specific request to is data space allocated by a specific request to allocate; e.g. the C function allocate; e.g. the C function malloc()malloc() command. Some command. Some languages (Ada, Java) use a built-in languages (Ada, Java) use a built-in newnew operator to acquire operator to acquire heap space. Space can be de-allocated (freed) automatically heap space. Space can be de-allocated (freed) automatically (Java) or by the explicit command (Java) or by the explicit command free()free(). On a run-time . On a run-time system requiring a programmer to code the system requiring a programmer to code the free()free() action, it is action, it is possible to “forget” to free, and consequently such memory possible to “forget” to free, and consequently such memory space will never be returned until main() exit.space will never be returned until main() exit.

We shall build graphs using heap space.We shall build graphs using heap space.

Page 5: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

5

Formal Definition of GraphEmpty GraphEmpty Graph: For expediency we ignore the possibility of a graph G being empty; in : For expediency we ignore the possibility of a graph G being empty; in

that case the data structure that points to the first node is simply NILthat case the data structure that points to the first node is simply NIL

GraphGraph: A graph G is a data structure : A graph G is a data structure G = { V, E } G = { V, E } consisting of a set consisting of a set EE of edges of edges and a set of and a set of VV vertices, AKA nodes. Any node vertices, AKA nodes. Any node vvii ϵϵ V V may be connected to any may be connected to any other node other node vvjj. Such a connection is called an edge. Edges may be directed, or . Such a connection is called an edge. Edges may be directed, or even bi-directed. Different from a tree, a node in G may have any number of even bi-directed. Different from a tree, a node in G may have any number of predecessors –or incident edgespredecessors –or incident edges

Connected GraphConnected Graph: If all n > 0 nodes v: If all n > 0 nodes vnn in G are connected somehow, the graph G is in G are connected somehow, the graph G is called connected, regardless of edge directionscalled connected, regardless of edge directions

Strongly Connected ComponentStrongly Connected Component: A subset SG : A subset SG ⊆⊆ G is strongly connected, if every i > 0 G is strongly connected, if every i > 0 nodes vnodes vi i in SG can reach all vin SG can reach all vi i nodes in SG somehownodes in SG somehow

Directed Acyclic Graph Directed Acyclic Graph (DAG): A DAG is a graph with directed edges but no cycles. A (DAG): A DAG is a graph with directed edges but no cycles. A node may still have multiple predecessorsnode may still have multiple predecessors

When programming graphs, it is convenient to add fields to the node type for auxiliary When programming graphs, it is convenient to add fields to the node type for auxiliary functions; e.g. it is possible to process all nodes in a linear fashion by adding a functions; e.g. it is possible to process all nodes in a linear fashion by adding a link field, often called a “finger”link field, often called a “finger”

Sample 1: building a stack of all nodes in G; used in SCC, not covered in CS 201 Sample 2: traversing all nodes in G, though G is unconnected!

Page 6: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

6

Building a Graph

Page 7: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

7

Graph Data StructureA graph G( v, e ) consists of nodes v and edges eA graph G( v, e ) consists of nodes v and edges e

Implemented via some node_type data structure

G is identified and thus accessible via one select node, G is identified and thus accessible via one select node, called called entry nodeentry node, or simply entry, AKA , or simply entry, AKA headhead

Head is of type Head is of type pointer to node_typepointer to node_type

G is not necessarily connectedG is not necessarily connected If parts of G are unconnected, how can they be retrieved in

case of a necessary, complete graph traversal?

Several methods of forcing complete access:Several methods of forcing complete access: Either create a super-node, not specified by the user of G, in

a way that each unconnected region is pointed at Or have a linked-list (LL) meandering through each node of

G, without this link field being part of G proper; e.g. “finger”

Page 8: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

8

Sample Graph G0

1R

2

Y

3

G

5

B 4O

V

6

G0

Page 9: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

9

Graph Data StructureSample Graph GSample Graph G00 above has 6 nodes above has 6 nodesThe ID, AKA The ID, AKA namename of each node is shown next to the of each node is shown next to the

nodes, e.g. 1 2 3 4 5 …nodes, e.g. 1 2 3 4 5 …The graphThe graph’’s node type data structure includes such name s node type data structure includes such name

information as part of information as part of node_typenode_typeIn addition, each node in GIn addition, each node in G00 has has attributesattributes, such as R, G, , such as R, G,

Y etc. in the sample aboveY etc. in the sample aboveThere may be many more attributes belonging to each There may be many more attributes belonging to each

node, depending on what the graph is used fornode, depending on what the graph is used forAny of these attributes must also be declared in the Any of these attributes must also be declared in the

node_typenode_type data structure data structureSuccessors, if any, of each node must be encoded in the Successors, if any, of each node must be encoded in the

node somehow; there is no upper limit on the number!node somehow; there is no upper limit on the number!GG00 has 3 SCCs; 2 of those are trivial, thus not interesting! has 3 SCCs; 2 of those are trivial, thus not interesting!

Page 10: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

10

Graph Data StructureSince in general there is no inherent upper bound for Since in general there is no inherent upper bound for

any node on the number of successor nodes, a any node on the number of successor nodes, a suitable way to define successors is via a linked listsuitable way to define successors is via a linked list

Thus a possible data type for successor nodes is a Thus a possible data type for successor nodes is a pointer to a link nodepointer to a link node

Link nodes can be allocated off the heap, as needed; Link nodes can be allocated off the heap, as needed; they are not of type they are not of type node_typenode_type, but of , but of link_typelink_type

And each link consists of just 2 fieldsAnd each link consists of just 2 fields One field pointing to the next link; the type is pointer to link_type, in some languages expresses as *link_type

The other field pointing to the successor node; the type is pointer to node_type

For convenience, the last link inserted is added at the For convenience, the last link inserted is added at the head of the list, saving multiple searches for list endhead of the list, saving multiple searches for list end

Page 11: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

11

Graph Data Structure, Link// node may have any number of successors// node may have any number of successors// all need to be retrievable.// all need to be retrievable.// so each node in G has a link pointer,// so each node in G has a link pointer,// pointing to LL of all successor nodes.// pointing to LL of all successor nodes.// Last one connected is the first one inserted// Last one connected is the first one inserted

typedef struct link_tp * link_ptr_tp;typedef struct link_tp * link_ptr_tp; // forward ref// forward ref

typedef struct link_tptypedef struct link_tp{{

link_ptr_tplink_ptr_tp next_link;next_link; // point to next link// point to next linknode_ptr_tpnode_ptr_tp next_node;next_node; // point to successor node// point to successor node

} str_link_tp;} str_link_tp;

#define LINK_SIZE sizeof( str_link_tp )#define LINK_SIZE sizeof( str_link_tp )

Page 12: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

12

Graph Data Structure, Node// "name" is arbitrary number given during creation// "name" is arbitrary number given during creation// "link" is head of LList of successor nodes, while// "link" is head of LList of successor nodes, while// // ""finger" is linear link through all nodesfinger" is linear link through all nodes// "visited" is true if was visited; initially FALSE// "visited" is true if was visited; initially FALSE

typedef struct node_tp * node_ptr_tp;typedef struct node_tp * node_ptr_tp; typedef struct node_tptypedef struct node_tp{{

link_ptr_tplink_ptr_tp link;link; // points to LL of successors// points to LL of successorsnode_ptr_tpnode_ptr_tp finger;finger; // finger through all nodes// finger through all nodesintint name;name; // name given at creation// name given at creationboolbool visited; visited; // to check connectivity// to check connectivityothers ...others ... // other fields: attributes// other fields: attributes

} str_node_tp;} str_node_tp;

#define NODE_SIZE sizeof( str_node_tp )#define NODE_SIZE sizeof( str_node_tp )

Page 13: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

13

Building a Graph, one Node// // create a node create a node in graph G, identified by “name”in graph G, identified by “name”// connect to the global “finger” at head of LList// connect to the global “finger” at head of LList// assumption: // assumption: no such node “name” exists in graphno such node “name” exists in graphnode_ptr_tp make_node( int name )node_ptr_tp make_node( int name ){ // make_node{ // make_node

node_ptr_tp node = (node_ptr_tp) malloc( NODE_SIZE );node_ptr_tp node = (node_ptr_tp) malloc( NODE_SIZE );

// check once non-Null, not repeatedly on user side!// check once non-Null, not repeatedly on user side!ASSERT( node, "space for node missing" );ASSERT( node, "space for node missing" );node->fingernode->finger = finger; = finger; // re-assign glob // re-assign glob finger!!finger!!node->linknode->link = NULL; = NULL; // pointer type// pointer typenode->namenode->name = name; = name; // IDs this node// IDs this nodenode->visitednode->visited = FALSE;= FALSE; // initially// initiallyfingerfinger = node;= node; // now link to “this”// now link to “this”return node;return node;

} //end make_node} //end make_node

Page 14: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

14

Building a Graph from Pairs// input is list of pairs, each element being a node name// input is list of pairs, each element being a node name// // craft edge from first to second craft edge from first to second name = numbername = number// If a node is new: create it; else use ptr = exists()// If a node is new: create it; else use ptr = exists()while( scanf( "%d%d", &a, &b ) ) { while( scanf( "%d%d", &a, &b ) ) { // a, b are ints// a, b are ints

if ( ! ( first = exists( a ) ) ) {if ( ! ( first = exists( a ) ) ) { // ‘a’ new node?// ‘a’ new node?first = make_node( a );first = make_node( a ); // allocate ‘a’// allocate ‘a’} //end if} //end ifif ( ! ( second = exists( b ) ) ) {if ( ! ( second = exists( b ) ) ) { // ‘b’ new node?// ‘b’ new node?second = make_node( b );second = make_node( b ); // allocate ‘b’// allocate ‘b’} //end if} //end if// both exist. Either created, or pre-existed: Connect!// both exist. Either created, or pre-existed: Connect!if ( new_link( first, second ) ) {if ( new_link( first, second ) ) {link = make_link( first->link, second );link = make_link( first->link, second );ASSERT( link, "no space for link node" );ASSERT( link, "no space for link node" );first->link = link;first->link = link;}else{}else{// link was there already, no need to add again!// link was there already, no need to add again!printf( "<><> skip duplicate link %d->%d\n", a, b );printf( "<><> skip duplicate link %d->%d\n", a, b );} //end if} //end if

} //end while} //end while

Page 15: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

15

Building a Graph from Pairs// // check, whether link between 2 nodes already existscheck, whether link between 2 nodes already exists// if not, return true: New! Else return false, NOT new!// if not, return true: New! Else return false, NOT new!bool new_link( node_ptr_tp first, node_ptr_tp second )bool new_link( node_ptr_tp first, node_ptr_tp second ){ // new_link{ // new_link

int targetint target = second->name;= second->name;link_ptr_tp linklink_ptr_tp link = first->link;= first->link;

while ( link ) {while ( link ) {if ( target == link->next_node->name ) {if ( target == link->next_node->name ) {

return FALSE; // it is an existing link, NOT newreturn FALSE; // it is an existing link, NOT new} //end if} //end if// check next node; if any// check next node; if anylink = link->next_link;link = link->next_link;

} //end while} //end while// none of successors equal the second node's name// none of successors equal the second node's namereturn TRUE; return TRUE; // is a new link // is a new link

} //end new_link} //end new_link

Page 16: CS 201 Computer Systems Programming Chapter 14 “ Heap Space and Graphs ”

16

References1.1. Control Flow Graph, in: Control Flow Graph, in: Mayer, H. Mayer, H. ““Parallel Parallel

Execution Enabled by Refined Source Analysis: Execution Enabled by Refined Source Analysis: Cost and Benefits in a Supercompiler”Cost and Benefits in a Supercompiler”, R. , R. Oldenbourg Verlag München/Wien, March 1997 Oldenbourg Verlag München/Wien, March 1997

2.2. Graphs in: Graphs in: C. Berge, C. Berge, ““Graphs and HypergraphsGraphs and Hypergraphs””, , North-Holland, Amsterdam 1973North-Holland, Amsterdam 1973

3.3. SCCs: Robert Tarjan,SCCs: Robert Tarjan, "Depth-First Search and Linear "Depth-First Search and Linear Graph Algorithms"Graph Algorithms". SIAM J. Computing, Vol. 1, No. . SIAM J. Computing, Vol. 1, No. 2, June 19722, June 1972