PRESENTED TO: MAM MERYAM DEPARTMENT OF COMPUTER SCIENCE
GROUP MEMBERS : IFRA ILYAS (470) AQSA SHAUKAT (586) PAZEER ZARA (452) SAMRA ASLAM (427) AQSA ANWAR (453)
SHORTEST PATH PROBLEM
Graph : A graph is a representation of a set of objects where some pairs of objects are connected by links. Vertices : The interconnected objects are represented by mathematical abstractions called vertices . Edges: The links that connect some pairs of vertices are called edges .
SHORTEST PATH problem : The shortest path problem is the problem of finding a path between two vertices in a graph such that the sum of the weights of its constituent edges is minimized.
Types of graphs: Weighted : A graph is a weighted graph if a number (weight) is assigned to each edge. Example: Such weights might represent, for example, costs, lengths or capacities, etc. Un-weighted: A graph is a un-weighted graph if a number(weight) is not assigned to each edge.
Types of graphs: Directed: An directed graph is one in which edges have orientation. Un-directed: An undirected graph is one in which edges have no orientation.
SHORTEST PATH PROBLEM: Given the graph below, suppose we wish to find the shortest path from vertex 1 to vertex 13.
EXAMPLE: After some consideration, we may determine that the shortest path is as follows, with length 14 Other paths exists, but they are longer
Negative Cycles: Clearly, if we have negative vertices, it may be possible to end up in a cycle whereby each pass through the cycle decreases the total length Thus, a shortest length would be undefined for such a graph Consider the shortest path from vertex 1 to 4... We will only consider non- negative weights.
Shortest Path Example: Given: Weighted Directed graph G = (V, E). Source s , destination t . Find shortest directed path from s to t . s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 Cost of path s-2-3-5-t = 9 + 23 + 2 + 16 = 48.
Discussion Items How many possible paths are there from s to t ? Can we safely ignore cycles? If so, how? Any suggestions on how to reduce the set of possibilities? s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6
Key Observation: A key observation is that if the shortest path contains the node v , then: It will only contain v once, as any cycles will only add to the length. The path from s to v must be the shortest path to v from s . The path from v to t must be the shortest path to t from v .
Dijkstra’s algorithm: Works when all of the weights are positive. Provides the shortest paths from a source to all other vertices in the graph. Can be terminated early once the shortest path to t is found if desired.
Example : Consider the graph: the distances are appropriately initialized all vertices are marked as being unvisited
Example : Visit vertex 1 and update its neighbours , marking it as visited the shortest paths to 2, 4, and 5 are updated
Example : The next vertex we visit is vertex 4 vertex 5 1 + 11 ≥ 8 don’t update vertex 7 1 + 9 < ∞ update vertex 8 1 + 8 < ∞ update