+ All Categories
Home > Documents > CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Date post: 19-Dec-2015
Category:
View: 218 times
Download: 0 times
Share this document with a friend
26
CS 240: Data CS 240: Data Structures Structures Tuesday, July 31 Tuesday, July 31 st st Graphs, Applications of Graphs, Applications of Previous Data Structures Previous Data Structures
Transcript
Page 1: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

CS 240: Data StructuresCS 240: Data Structures

Tuesday, July 31Tuesday, July 31stst

Graphs, Applications of Previous Graphs, Applications of Previous Data StructuresData Structures

Page 2: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Remaining ReadingsRemaining Readings Chapter 7-7.3 (up to p.426)Chapter 7-7.3 (up to p.426) P. 435 on backtrackingP. 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 This portion is a bit excessive, only look at what you need to knowneed 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)

Page 3: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.
Page 4: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Start

Page 5: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

RepresentationRepresentation

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 Graphs will often take distance between Nodes into account (so far, our distances have been into account (so far, our distances have been irrelevant)irrelevant) Hence each Node* is associated with a distanceHence each Node* is associated with a distance

We can store this as a “pair<Node*,int>”We can store this as a “pair<Node*,int>” Requires #include<algorithm>Requires #include<algorithm>

Page 6: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Linked ListLinked 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 == It has nodes with only 1 Node* (list size ==

1)1)And the distance between each Node is And the distance between each Node is

the same (no value is needed, but we the same (no value is needed, but we might as well say 0).might as well say 0).

Page 7: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Binary TreesBinary Trees

A Binary Tree is a subset of graphA Binary Tree is a subset of graphEach node has 2 Node*Each node has 2 Node*A node in a tree always points to a node A node in a tree always points to a node

closer to the bottom of the tree. There are closer to the bottom of the tree. There are no cycles!no cycles!

Page 8: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

N TreesN Trees

Just like binary trees, but up to N Node*Just like binary trees, but up to N Node*

Page 9: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

So, what is a graph?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 Node pointers may be associated with a

distance.distance.Cycles could occur!Cycles could occur!

Page 10: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Node:Node:

Our node needs:Our node needs:

T data;T data;A list of Node<T>*A list of Node<T>*A list of associated distancesA list of associated distances

Page 11: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

RepresentationsRepresentations

Let’s look at locations as a representation Let’s look at locations as a representation of a graph.of a graph.

Page 12: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Alternate RepresentationsAlternate Representations

Adjacency MatrixAdjacency MatrixWhat if the graph is small?What if the graph is small?

Page 13: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Alternate RepresentationsAlternate Representations

Adjacency ListAdjacency List

Page 14: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

HashingHashing

A hash can be represented with a graph.A hash can be represented with a graph.

Page 15: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

TermsTerms

Out-DegreeOut-Degree In-DegreeIn-DegreeCycleCycleDirectedDirectedUndirectedUndirected

Page 16: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

InsertionInsertion

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?

Page 17: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

RemovalRemoval

Removal requires us to update every node Removal requires us to update every node that points to us.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 On a directed graph, we don’t know who

points to a particular node!points to a particular node!

Page 18: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

CopyingCopying

Copying Nodes is no longer trivial.Copying Nodes is no longer trivial.Generally, a graph will maintain or create Generally, a graph will maintain or create

an adjacency matrix/list in order to transfer an adjacency matrix/list in order to transfer the appropriate information.the appropriate information.

We could travel each of the nodes and We could travel each of the nodes and copy them one at a time. However, linking copy them one at a time. However, linking them together in this manner is arduous.them together in this manner is arduous.

Page 19: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

FirstFirst

A graph may no longer has a concept of a A graph may no longer has a concept of a “first” node.“first” node.

It may be reasonable to be able to start It may be reasonable to be able to start anywhere. However, that is not always the anywhere. However, that is not always the case.case.

Therefore, we need to be able to travel the Therefore, we need to be able to travel the graph.graph.

Page 20: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

DestructionDestruction

Start at some node (X), find some Start at some node (X), find some destination (Y). Graph::remove(X), repeat destination (Y). Graph::remove(X), repeat this at Y (X=Y, start over) unless Y is this at Y (X=Y, start over) unless Y is NULL.NULL.

Page 21: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

SearchingSearching

Searching and traversing are more difficult Searching and traversing are more difficult because of cycles.because of cycles.

Page 22: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Depth-firstDepth-first

Starting at “first”Starting at “first”At a node:At a node:

Select the next destination and go it to (if you Select the next destination and go it to (if you haven’t been there already).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”.

Page 23: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Breadth-FirstBreadth-First

Starting at “first”Starting at “first” At a node:At a node:

Place all destinations in a queue (if you Place all destinations in a queue (if you haven’t been to them before).haven’t been to them before).

Dequeue and go to that destination (if you Dequeue and go to that destination (if you haven’t been to that location) until emptyhaven’t been to that location) until empty

What happens if you apply this to a binary What happens if you apply this to a binary tree?tree?

Page 24: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Applications of ADTsApplications of ADTs

How do we create the following?How do we create the following?A schedule of tasksA schedule of tasksA schedule of tasks with priority (like an A schedule of tasks with priority (like an

OS)OS)An activation record for recursive An activation record for recursive

functions/methodsfunctions/methods

Page 25: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

Questions?Questions?

… … there is still more, but I’m hinting that there is still more, but I’m hinting that you should ask questions now…you should ask questions now…

Hint, hint.Hint, hint.

Page 26: CS 240: Data Structures Tuesday, July 31 st Graphs, Applications of Previous Data Structures.

A challenger appears!A challenger appears!

Prepare for a quiz!Prepare for a quiz!


Recommended