weighted graphs - edoras.sdsu.eduhealey/cs310/references/weighted_graphs.pdf · -- roy osherove,...
TRANSCRIPT
WEIGHTED GRAPHSCS 310, SAN DIEGO STATE UNIVERSITY
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
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
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
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
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
DIJKSTRA’S
2 2
3 Inf
4 Inf
5 7
6 Inf
122
35
2
20
27
Unvisited
1 0
Visited
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
DIJKSTRA’S
3 7
4 7
6 Inf
122
35
2
20
27
Unvisited
1 0
2 2
5 4
Visited
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
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>
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� �
WRAPUP
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?!”
VERSION CONTROL
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
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
GUI SOLUTIONS
https://www.git-scm.com/downloads/guis
BUILT IN IDE SUPPORT
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
GETTING STARTED WITH GIT
1. Download Git
2. ????
3. Profit!
http://www.git-scm.com/book/en/v1/Getting-Started-About-Version-Control
XKCD
GIT COMMANDS
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.
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
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
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
GETTING STARTED
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
VICTORY
http://www.joeylogano.com/wp-content/gallery/campingworld-com-500/15TAL2ck08069W.jpg