750 likes | 1.86k Views
Introduction to Graphs. Graphs: what are they?. Representations of pairwise relationships Collections of objects under some specified relationship. Graphs: what are they mathematically?. A graph G is a pair (V,E) V is a set of vertices (nodes) E is a set of pairs ( a,b ), a,b V
E N D
Graphs: what are they? • Representations of pairwise relationships • Collections of objects under some specified relationship
Graphs: what are they mathematically? • A graph G is a pair (V,E) • V is a set of vertices (nodes) • E is a set of pairs (a,b), a,b V • V is the set of relatable objects • E is the set of relationships
A Visual Example 1 3 2 4 5 G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} )
Directed Graphs • In a directed graph • (a,b) E does not imply (b,a) E • Undirected graphs are a subset • (a,b) E if and only if (b,a) E • Visually, directed graphs are drawn with arrows
Directed Graph Example 1 3 2 4 5 G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } )
Weighted Graphs • Have weights associated with edges • Can be directed or undirected • Can have pairs, in a directed graph, where the weights from (a,b) have no relationship on the weights from (b,a)
Weighted Graph Example 1 3.2 3 2 π 42 666 -5 777 4 5 G = ( {1,2,3,4,5}, { (1,5,-5), (2,1,3.2), (2,3,42), (3,2, π), (2,4,777), (4,1,666) } )
Graph Representation • How to represent in memory? • Two common ways: • Adjacency Lists • Adjacency Matrix
Undirected Graph Example G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} ) 1 2 3 4 5 2 4 5 1 3 4 2 1 2 1
Directed Graph Example G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } ) 1 2 3 4 5 5 1 3 4 2 1
Undirected Graph Example G = ( {1,2,3,4,5}, {(1,2), (1,4), (2,3), (2,4), (1,5)} )
Directed Graph Example G = ( {1,2,3,4,5}, { (1,5), (2,1), (2,3), (2,4), (3,2), (4,1) } )
DFS Algorithm • Algorithm DFS ( G ) for i = 1 to n do // Initialize all vertices are unvisited status[i] = unvisited parent[i] = NULL for i = 1 to n do if (status[i] == unvisited) // If there exits an invisited vertex, start traversal DF-Travel(i) Algorithm DF-Travel ( v ) status[v] = visited for each vertex u adjacent to v do if status[u] == unvisited then parent[u] = v DF-Travel ( u )
Breadth First Search Algorithm • Breadth First Search (BSF): • Initially all vertices of the graph are unvisited. • Start visiting the graph from any vertex, say v . • Visit each unvisited adjacent vertex of v. • Repeat the process for each vertex visited. • This process stops when all vertices reachable from v are visited. • This method is called breadth first search, since it works outward from a center point, much like the ripples created when throwing a stone into a pond. It moves outward in all directions, one level at a time.
What is the Minimum Spanning tree (MST)? • Consider Oil Well example.=> • Resultant graph is a tree which spannes all vertices of the graph. Hence called spanning tree. • The spanning tree with minimum cost is called Minimum Spanning tree (MST).
Minimum Spanning Tree • Required: Connect all nodes at minimum cost. • Cost is sum of edge weights • Can start at any node • Unique solution. • Can come from different sets of edges • Two algorithms • Prim’s • Kruskal’s
Finding a MST • Principal greedy methods: algorithms by Prim and Kruskal • Prim • Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree • Kruskal • Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far
Prim’sAlgorithm Start with any vertex. All its edges are candidates. Stop looking when all vertices are picked Otherwise repeat… Pick minimum edge (no cycles) and connect the adjacent vertex Add all edges from that new vertex to our “candidates”
The Shortest Path Problem • Given a directed, weighted graphG = ( V, E ) • What is the shortest path from the start vertex to some end vertex? • Minimize the sum of the edge weights
Dijkstra's Shortest Path Algorithm • In a graph in which edges have costs .. • Find the shortest path from a source to a destination Surprisingly .. • While finding the shortest path from a source to one destination, we can find the shortest paths to all over destinations as well !
Dijkstra Algorithm • Problem: • From a given source vertex s ∈ V, find the shortest-path weights d(s, v)for all v∈V. • IDEA: Greedy. • Maintain a set S of vertices whose shortest-path distances from s are known. • At each step add to S the vertex v ∈ V –S whose distance estimate from s is minimal. • Update the distance estimates of vertices adjacent to v.
Dijkstra's Shortest Path Algorithm • For a graph G = ( V, E ) • Dijkstra’s algorithm keeps two sets of vertices: • S Vertices whose shortest paths have already been determined • V-S Remainder • Also • d Best estimates of shortest path to each vertex • Π Predecessors for each vertex
Dijkstra's Shortest Path Algorithm • Find shortest path from s to t.