1 / 16

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

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.

venice
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Syllabus • Goal • Heap Space • Definition of Graph • Building a Graph • Graph Data Structure • References SCC not covered here

  3. Goal • Improve students’ understanding and use of dynamic memory management: using the system heap to allocate and free data space • Sometimes heap usage is explained by using and exercising linked lists that can grow and shrink dynamically • For a system programmer that would be too simple; so we shall exercise heap management by building directed, cyclic graphs • Our graphs may be unconnected and each vertex may have any number of successor nodes

  4. Heap Space • Static data space of a program exists during whole execution; created at program load time, returned at program exit() time. • Automatic space is data space created by the run-time system. Each time a new procedural (or function) scope is entered, automatic space is allocated off the stack (could also be from the heap). Part of the return operation is to free such previously granted automatic space. • Controlled space is data space allocated by a specific request to allocate; e.g. the C function malloc() command. Some languages (Ada, Java) use a built-in new operator to acquire heap space. Space can be de-allocated (freed) automatically (Java) or by the explicit command free(). On a run-time system requiring a programmer to code the free() action, it is possible to “forget” to free, and consequently such memory space will never be returned until main() exit. • We shall build graphs using heap space.

  5. Formal Definition of Graph • Empty Graph: 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 NIL • Graph: A graph G is a data structure G = { V, E } consisting of a set E of edges and a set of V vertices, AKA nodes. Any node viϵ V may be connected to any other node vj. 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 predecessors –or incident edges • Connected Graph: If all n > 0 nodes vn in G are connected somehow, the graph G is called connected, regardless of edge directions • Strongly Connected Component: A subset SG ⊆ G is strongly connected, if every i > 0 nodes vi in SG can reach all vi nodes in SG somehow • Directed Acyclic Graph (DAG): A DAG is a graph with directed edges but no cycles. A node may still have multiple predecessors • 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 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!

  6. Building a Graph

  7. Graph Data Structure A 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, called entry node, or simply entry, AKA head Head is of type pointer to node_type G 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: • 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”

  8. Sample Graph G0 G0 2 1 3 R Y G B O 4 5 V 6

  9. Graph Data Structure Sample Graph G0 above has 6 nodes The ID, AKA name of each node is shown next to the nodes, e.g. 1 2 3 4 5 … The graph’s node type data structure includes such name information as part of node_type In addition, each node in G0 has attributes, such as R, G, Y etc. in the sample above There may be many more attributes belonging to each node, depending on what the graph is used for Any of these attributes must also be declared in the node_type data structure Successors, if any, of each node must be encoded in the node somehow; there is no upper limit on the number! G0 has 3 SCCs; 2 of those are trivial, thus not interesting!

  10. Graph Data Structure Since in general there is no inherent upper bound for any node on the number of successor nodes, a suitable way to define successors is via a linked list Thus a possible data type for successor nodes is a pointer to a link node Link nodes can be allocated off the heap, as needed; they are not of type node_type, but of link_type And 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 head of the list, saving multiple searches for list end

  11. Graph Data Structure, Link // node may have any number of successors // all need to be retrievable. // so each node in G has a linkpointer, // pointing to LL of all successor nodes. // Last one connected is the first one inserted typedefstructlink_tp * link_ptr_tp; // forward ref typedefstructlink_tp { link_ptr_tpnext_link; // point to next link node_ptr_tpnext_node; // point to successor node } str_link_tp; #define LINK_SIZE sizeof( str_link_tp )

  12. Graph Data Structure, Node • // "name" is arbitrary number given during creation • // "link" is head of LList of successor nodes, while • // "finger" is linear link through all nodes • // "visited" is true if was visited; initially FALSE • typedef struct node_tp * node_ptr_tp; • typedef struct node_tp • { • link_ptr_tp link; // points to LL of successors • node_ptr_tp finger; // finger through all nodes • int name; // name given at creation • bool visited; // to check connectivity • others ... // other fields: attributes • } str_node_tp; • #define NODE_SIZE sizeof( str_node_tp )

  13. Building a Graph, one Node • // create a node in graph G, identified by “name” • // connect to the global “finger” at head of LList • // assumption: no such node “name” exists in graph • node_ptr_tp make_node( int name ) • { // make_node • node_ptr_tp node = (node_ptr_tp) malloc( NODE_SIZE ); • // check once non-Null, not repeatedly on user side! • ASSERT( node, "space for node missing" ); • node->finger = finger; // re-assign glob finger!! • node->link = NULL; // pointer type • node->name = name; // IDs this node • node->visited = FALSE; // initially • finger = node; // now link to “this” • return node; • } //end make_node

  14. Building a Graph from Pairs • // input is list of pairs, each element being a node name • // craft edge from first to second name = number • // If a node is new: create it; else use ptr = exists() • while( scanf( "%d%d", &a, &b ) ) { // a, b are ints • if ( ! ( first = exists( a ) ) ) { // ‘a’ new node? • first = make_node( a ); // allocate ‘a’ • } //end if • if ( ! ( second = exists( b ) ) ) { // ‘b’ new node? • second = make_node( b ); // allocate ‘b’ • } //end if • // both exist. Either created, or pre-existed: Connect! • if ( new_link( first, second ) ) { • link = make_link( first->link, second ); • ASSERT( link, "no space for link node" ); • first->link = link; • }else{ • // link was there already, no need to add again! • printf( "<><> skip duplicate link %d->%d\n", a, b ); • } //end if • } //end while

  15. Building a Graph from Pairs • // check, whether link between 2 nodes already exists • // if not, return true: New! Else return false, NOT new! • bool new_link( node_ptr_tp first, node_ptr_tp second ) • { // new_link • int target = second->name; • link_ptr_tp link = first->link; • while ( link ) { • if ( target == link->next_node->name ) { • return FALSE; // it is an existing link, NOT new • } //end if • // check next node; if any • link = link->next_link; • } //end while • // none of successors equal the second node's name • return TRUE; // is a new link • } //end new_link

  16. References • Control Flow Graph, in: Mayer, H. “Parallel Execution Enabled by Refined Source Analysis: Cost and Benefits in a Supercompiler”, R. Oldenbourg Verlag München/Wien, March 1997 • Graphs in: C. Berge, “Graphs and Hypergraphs”, North-Holland, Amsterdam 1973 • SCCs: Robert Tarjan, "Depth-First Search and Linear Graph Algorithms". SIAM J. Computing, Vol. 1, No. 2, June 1972

More Related