weighted graphs - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- roy osherove,...

30
WEIGHTED GRAPHS CS 310, SAN DIEGO STATE UNIVERSITY

Upload: others

Post on 12-Mar-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

WEIGHTED GRAPHSCS 310, SAN DIEGO STATE UNIVERSITY

Page 2: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

OVERVIEW

Unweighted

Simply reflect adjacency

Sufficient where cost is meaningless or does not vary

Moving through a maze

Family trees

Weighted

Weighted graphs allow us to intelligently model paths

GPS guidance adapting to traffic congestion

Network routers finding efficient paths from machine to machine

Page 3: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

EDGES: WEIGHT

Associates a cost with moving from one vertex to the next through that edge

Edge weights can express:

Ping times between routers

Resistance in a circuit

Number of calories expended

Difficulty of the terrain

Units of radiation received

Autocorrect Image: https://en.wikipedia.org/wiki/Loop_graph_theory

126

35

2

20

27

Page 4: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

SHORTEST PATH

Associates a cost with moving from one vertex to the next through that edge

Edge weights can express:

Ping times between routers

Resistance in a circuit

Number of calories expended

Difficulty of the terrain

Units of radiation received

Autocorrect

https://msdn.microsoft.com/en-us/library/ms379574v=vs.80.aspx

Page 5: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

DIJKSTRA’S ALGORITHM1. Initialize every node with a

starting distance:

Zero for the starting node

Infinity for all others

2. Mark all nodes as unvisited and add them to the unvisited set

3. Consider the current node’s neighbors in the unvisited set:

Calculate tentative distance

Update distance only if smaller

4. After evaluating every neighbor, remove the current node from the unvisited set

5. Terminate if either:

Destination reached

Smallest distance in unvisited set is infinity

6. Otherwise, select the unvisited node with the smallest distance and continue from step 3

Page 6: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

DIJKSTRA’S INITIALIZATION

1 0

2 Inf

3 Inf

4 Inf

5 Inf

6 Inf

122

35

2

20

27

Finding shortest path from 1 to 6

Page 7: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

DIJKSTRA’S

2 2

3 Inf

4 Inf

5 7

6 Inf

122

35

2

20

27

Unvisited

1 0

Visited

Page 8: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

DIJKSTRA’S

3 7

4 Inf

5 4

6 Inf

122

35

2

20

27

Unvisited

1 0

2 2

Visited

Old distance to 5: 7New distance to 5: 2+2=4

Maintain node’s parent link in tree to keep the shortest path

Page 9: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

DIJKSTRA’S

3 7

4 7

6 Inf

122

35

2

20

27

Unvisited

1 0

2 2

5 4

Visited

Page 10: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

DIJKSTRA’S

4 7

6 Inf12

2

35

2

20

27

Unvisited

1 0

2 2

5 4

3 7

Visited

Because Node 3’s path to node 4 carries a weight of 9, it is not the shortest path, and the tentative distance remains the same

Page 11: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

DIJKSTRA’S

6 1912

2

35

2

20

27

Unvisited

1 0

2 2

5 4

3 7

4 7

Visited

By tracing back the parent pointers, the shortest path: <1,2,5,4,6>

Page 12: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

DIJKSTRA’S RUNNING TIME

Dependent upon the number of Edges and Vertices in the graph

For a fully connected graph, the number of edges is:

Dijkstra’s Complexity:

Where Tdk represents the complexity of decreasing the key and Tem is the complexity of extracting the minimum from the unvisited neighbors

Choice for the backing data structure impacts Dijkstra’s performance

2( )E O V

( )dk emO E T V T� �

Page 13: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

WRAPUP

Page 14: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

VERSION CONTROL

A tool to protect you from yourself and other developers

“This was working yesterday! What happened!”

“Who introduced this bug?”

“What versions are effected?!”

Page 15: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

VERSION CONTROL

Page 16: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

TWO MAJOR PHILOSOPHIES

CVS

Centralized Version control System

Clients communicate with a single, authoritative server

Changes stored at file level

Older model losing popularity

GIT

Distributed version control

Individuals “clone” repositories

Independently merge changes as necessary

Changes bundled at project level

Many people developing unique solutions from a standard code base, and these changes may, or may not, make it into the master version

Page 17: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

OFFSITE SERVERS

When developing on multiple computers, storing the repository on a server helps simplify code sharing

Rather than exchanging USB sticks, one only need send a link to the repository

However, must have an available server to make it work

Multiple commercial solutions offer server space for Git repositories, but one does not need to use any of them to use Git.

Github: Free, but without private servers, so the whole world may monitor your progress (and steal your grade)

Bitbucket: Free, but with a limited number of private repositories

With any commercial solution, the code resides on someone else’s server

Git works locally as well as remotely

Page 18: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

GUI SOLUTIONS

https://www.git-scm.com/downloads/guis

Page 19: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

BUILT IN IDE SUPPORT

Page 20: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

GIT FOLDER

Although one may use Github or Bitbucket for a common server solution, Git does not require this

At its base level, a Git repository is simply a project directory

If you want to get a colleague up to speed on a project, copy the entire repository

Every developer then possesses a complete project history The repository maintains the total

history, so if one adds a 1GB file, and then deletes it, the repository will still

hold the 1GB file

Page 21: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

GETTING STARTED WITH GIT

1. Download Git

2. ????

3. Profit!

http://www.git-scm.com/book/en/v1/Getting-Started-About-Version-Control

Page 22: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

XKCD

Page 23: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

GIT COMMANDS

Page 24: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

UNIT TESTING

“… a piece of code that invokes a unit of work and checks one specific end result of that unit of work.”

-- Roy Osherove, The Art of Unit Testing

Note: The intentional use of ‘Art’ in the title.

Page 25: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

JUNIT SUPPORT BUILT INTO ECLIPSE

Eclipse supports the Junit library

Clean interface provides modular feedback about the System Under Test (SUT)

Writing good unit tests takes time and practice.

An evolving philosophy

Page 26: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

EXAMPLES FROM YOUR HOMEWORK GRADERS

Tests run independently, and in an arbitrary order

Limit tests to one, and only one, assertion

When something fails, you want to know precisely what failed

Senseless to debug tests

Names should provide insight into the test being performed, and how it might fail

Page 27: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

INTEGRATION VS. UNIT

Integration Test

Uses the uncontrolled System.time, so every test is a new test

Accesses a real database on a file system (slow)

Uses the filesystem (read/write)

Uses “real” pseudo-random numbers

Potentially tests many things at once

Not necessarily repeatable (time changed!)

Unit Test

Uses a predefined, repeatable time

Uses a test database (in memory) with controlled entries

Simulates the file system

Seeds the random number generator with a known constant

Focused on a single element of work

100% repeatable

Page 28: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

GETTING STARTED

Page 29: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

FIRST TESTS

Eclipse stubs out the test class, and you are ready to start writing tests

Some possible tests:

Empty data structure properties

Adding an existing item to a map

Concurrent modification exception

If the Interface specifies a behavior, then TEST IT

Page 30: WEIGHTED GRAPHS - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- Roy Osherove, The Art of Unit Testing Note: The intentional use of ‘Art’ in the title. JUNIT

VICTORY

http://www.joeylogano.com/wp-content/gallery/campingworld-com-500/15TAL2ck08069W.jpg