Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 240: Data Structures Tuesday, July 31st Graphs, Applications of Previous Data Structures Remaining Readings Chapter 7-7.3 (up to p.426) P. 435 on backtracking Chap 8-8.5 (up to p.484) Section 8.6 (p.496-505) Chapter 11-11.3 (up to p.643) Section 11.4,11.5 (p.656-681) This portion is a bit excessive, only look at what you need to know Chapter 12-12.2 (up to p.697) Section 12.3 (p.701-703) Section 12.4 (p.715-727) Start Representation Now, our Node needs new data: A list of Node* instead of just “next” Graphs will often take distance between Nodes into account (so far, our distances have been irrelevant) Some way to select a “next” Hence each Node* is associated with a distance We can store this as a “pair<Node*,int>” Requires #include<algorithm> Linked List A Linked List is a subset of Graph. It has nodes with only 1 Node* (list size == 1) And the distance between each Node is the same (no value is needed, but we might as well say 0). Binary Trees A Binary Tree is a subset of graph Each node has 2 Node* A node in a tree always points to a node closer to the bottom of the tree. There are no cycles! N Trees Just like binary trees, but up to N Node* So, what is a graph? A graph is a set of Nodes with N Node*. However, the Node* has no limitations. Node pointers may be associated with a distance. Cycles could occur! Node: Our node needs: T data; A list of Node<T>* A list of associated distances Representations Let’s look at locations as a representation of a graph. Alternate Representations Adjacency Matrix What if the graph is small? Alternate Representations Adjacency List Hashing A hash can be represented with a graph. Terms Out-Degree In-Degree Cycle Directed Undirected Insertion To insert into a graph: We need to know where the node is going. Who points to it? Who does it point to? What are the associated distances? Removal Removal requires us to update every node that points to us. With an undirected graph, this is easy. On a directed graph, we don’t know who points to a particular node! Copying Copying Nodes is no longer trivial. Generally, a graph will maintain or create an adjacency matrix/list in order to transfer the appropriate information. We could travel each of the nodes and copy them one at a time. However, linking them together in this manner is arduous. First A graph may no longer has a concept of a “first” node. It may be reasonable to be able to start anywhere. However, that is not always the case. Therefore, we need to be able to travel the graph. Destruction Start at some node (X), find some destination (Y). Graph::remove(X), repeat this at Y (X=Y, start over) unless Y is NULL. Searching Searching and traversing are more difficult because of cycles. Depth-first Starting at “first” At a node: Select the next destination and go it to (if you haven’t been there already). Why does this work? Can you code this? This uses “backtracking”. Breadth-First Starting at “first” At a node: Place all destinations in a queue (if you haven’t been to them before). Dequeue and go to that destination (if you haven’t been to that location) until empty What happens if you apply this to a binary tree? Applications of ADTs How do we create the following? A schedule of tasks A schedule of tasks with priority (like an OS) An activation record for recursive functions/methods Questions? … there is still more, but I’m hinting that you should ask questions now… Hint, hint. A challenger appears! Prepare for a quiz!