170 likes | 178 Views
Explore the concepts of graphs, minimum spanning trees (MSTs), and partial order relations. Discover how graphs can represent direct road connections between locations and learn about different algorithms, including greedy algorithms, for solving graph-related problems.
E N D
Graphs and MSTs • Sections 1.4 and 9.1
Partial-Order Relations • Everybody is not related to everybody. Examples? • Direct road connections between locations • How about using a tree to represent the connections?
Edges (arcs) Vertices (nodes) {(v,v’) : v and v’ are in V} Graphs: Definition G = (V, E) Edges are a subset of V V We also write v v’ instead of (v.v’)
A directed graph or digraph is one were the order matters v v’ Directed Graphs Our PA map is undirected because if (v,v’) E then it is assumed that (v’,v) E (but not included in E) Can you think of a situation were the graph is directed? That is, we may have (v,v’) E but not (v’,v) E City streets. Some are one way
Complete Graphs A complete graph is one where there is an edge between every two nodes If the graph has N nodes and the graph is complete and undirected, how many edges are there? (N-1)N/2 If the digraph has N nodes and the digraph is complete, how many edges are there? (N-1)N
Every element in the left set is an element in the right set Subgraphs • Given a graph G = (V, E) and a graph G’ = (V’, E’), G is a subgraph of G’ if: • V V’ • E E’
Quiz: Obtain all the paths in the following digraph: 2 3 7 Paths Given a graph G = (V, E), a path between two elements v, v’ in V is a sequence of elements: v v1 v2 vN v’ … such that v v2,…, v1, v1 vN v’ are all edges in E The length of the path is the number of edges on it
Weighted Graphs A weighted graph G = (V, E), is a graph such that each edge v v’ has an associated number (called the weight) Given a weighted graph G = (V, E), a path between two elements v, v’ in V : v v1 … v’, the weight or cost of the path is the summation of the weight of the edges in the path
Connected? 2 3 7 5 Connected Graphs A graph G = (V, E) is connected if there is at least one path between any two elements Yes
Acyclic? 2 3 7 5 Acyclic Graphs Given a graph G = (V, E), a cycle is a path of at least length 1 starting with a node and ending in the same node A graph G = (V, E) is acyclic if it has no cycles No
Trees A tree is a connected acyclic graph G = (V, E) Number of arcs in a tree with |V| nodes: |E|-1
Techniques for Design of Algorithms • Brute Force: follows directly from the definition • Exhaustive search: particular form of Brute Force • Greedy Algorithm: Obtain solution by expanding partial solution constructed so far. On each step a decision is made that is: • Feasible • Locally optimal • Irrevocable
Example: Greedy algorithm for TSP Consider the following undirected graph: 2 D F 2 2 4 2 A 3 7 E 7 C 6 2 2 B 8 G 2 2 H Use a Greedy technique to obtain a solution of the TSP Complexity (n = # arcs)? O(n) Does using a Greedy technique always solves TSPs? No
The Minimum Spanning Tree • Give a graph G = (V, E), the minimum spanning tree (MST) is a weighted graph G’ = (V, E’) such that: • E’ E • G’ is connected • G’ has the minimum cost
V V’ Why does it works? Property: Suppose that we divide the nodes of a graph G = (V, E) in two groups V, V’: 12 22 7 20 20 5 6 66 10 13 If there is an edge e between the 2 groups with minimum cost, then there is an MST containing e
Visited Non visited Example of a “Greedy” Algorithm Dijkstra-Primis an example of a greedy algorithm since it looks at a subset of the larger problem 12 22 7 20 20 5 6 66 10 13 Unfortunately, not all problems can be solved with greedy algorithms Example of a non greedy algorithm? Our naïve algorithm for MST
… … e2 e1 Complexity • Actual complexity is O((|E|+|V|)log2 |E|) • It is easy to see that it is at most |E||V| e1 + (e1 + e2) +…+ (e1 + e2 + … +e2) |E||V|