Note: This course is a subset of our much longer course 'From 0 to 1: Data Structures & Algorithms' so please don't sign up for both:-)
This is an animated, visual and spatial way to learn data structures and algorithms
Note: This course is a subset of our much longer course 'From 0 to 1: Data Structures & Algorithms' so please don't sign up for both:-)
This is an animated, visual and spatial way to learn data structures and algorithms
Taught by a Stanford-educated ex-Googler.
The graph is a data structure that is used to model a very large number of real world problems. It's also an programming interview favorite. The study of graphs and algorithms associated with graphs forms an entire field of study called graph theory.
There are a number of different ways in which graphs can be implemented. However they all follow they same basic graph interface. The graph interface allows building up a graph by adding edges and traversing a graph by giving access to all adjacent vertices of any vertex.
Compare the adjacency matrix, adjacency list and the adjacency set in terms of space and time complexity of common operations
Common traversal methods of trees apply to graphs as well. There is an additional wrinkle with graphs, dealing with cycles and with unconnected graphs. Otherwise the algorithms are exactly the same as those we use to traverse trees.
So far we only deal with unweighted graphs. Graphs whose edges have a weight associated are widely used to model real world problems (traffic, length of path etc).
A greedy algorithm is one which tries to find the local optimum by looking at what is the next best step at every iteration. It does not look at the overall picture. It's best used for optimization problems where the solution is very hard and we want an approximate answer.
Finding the shortest path in a weighted graph is a greedy algorithm.
The implementation of Dijkstra's algorithm in Java.
A weighted graph can have edge weights which are negative. Dealing with negative weights have some quirks which are dealt with using the Bellman Ford algorithm.
If a graph has a negative cycle then it's impossible to find a shortest path as every round of the cycle makes the path shorter!
The minimal spanning tree is used when we want to connect all vertices at the lowest cost, it's not the shortest path from source to destination.
Let's see how we implement Prim's algorithm in Java.
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.