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