Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Similar presentations


Presentation on theme: "Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations."— Presentation transcript:

1 Graphs and MSTs Sections 1.4 and 9.1

2 Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations How about using a tree to represent the connections?

3 Graphs: Definition G = (V, E) Vertices (nodes) Edges (arcs) Edges are a subset of V  V {(v,v’) : v and v’ are in V} We also write v  v’ instead of (v.v’)

4 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 A directed graph or digraph is one were the order matters vv’

5 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

6 Subgraphs Given a graph G = (V, E) and a graph G’ = (V’, E’), G is a subgraph of G’ if: V  V’ E  E’ Every element in the left set is an element in the right set

7 Paths Given a graph G = (V, E), a path between two elements v, v’ in V is a sequence of elements: v Quiz: Obtain all the paths in the following digraph: 23 7 v1v1 … vNvN such that v v 1, v 1 v 2,…, v2v2 v’ vNvN are all edges in E The length of the path is the number of edges on it

8 Weighted Graphs 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 A weighted graph G = (V, E), is a graph such that each edge v  v’ has an associated number (called the weight)

9 Connected Graphs A graph G = (V, E) is connected if there is at least one path between any two elements 23 7 5 Connected? Yes

10 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 23 7 5 Acyclic? No

11 Trees A tree is a connected acyclic graph G = (V, E) Number of arcs in a tree with |V| nodes: |E|-1

12 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

13 Consider the following undirected graph: B C A H E D G F Example: Greedy algorithm for TSP 2 3 7 2 42 2 22 2 2 6 8 7 Use a Greedy technique to obtain a solution of the TSP Does using a Greedy technique always solves TSPs? Complexity (n = # arcs)?O(n) No

14 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

15 V V’ If there is an edge e between the 2 groups with minimum cost, then there is an MST containing e Why does it works? Property: Suppose that we divide the nodes of a graph G = (V, E) in two groups V, V’: 5 7 10 22 66 12 6 13 20

16 Visited Non visited Example of a non greedy algorithm? Example of a “Greedy” Algorithm Dijkstra-Prim is an example of a greedy algorithm since it looks at a subset of the larger problem 5 7 10 22 66 12 6 13 20 Our naïve algorithm for MST Unfortunately, not all problems can be solved with greedy algorithms

17 Complexity Actual complexity is O((|E|+|V|)log 2 |E|) It is easy to see that it is at most |E||V| … e1 e2 … e1 + (e1 + e2) +…+ (e1 + e2 + … +e2)  |E||V|


Download ppt "Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations."

Similar presentations


Ads by Google