Docsity

Graphs - Object Oriented Programming and Data Structures - Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Graphs - Object Oriented Programming and Data Structures - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Graphs

Time to do A4, Recursion

Histogram: max: 28. [00:02): 17 av: 5. [02:04): 102 median: 4. [04:06): 134 [06:08): 50 [08:10): 29 [10:12): 8 [12:14): 6 [14:16): 2 [16:18): 1 [18:30): 5 If you took more than 6- 7 hours for this assignment, you may have been wasting your time. A certain amount of “floundering”, just trying things, is good. But after a point, it just wastes time. Seek help if after an hour on one of the recursion problems you are stuck.

These are Graphs

K

5

K

3,

Applications of Graphs

 Communication networks  The internet is a huge graph  Routing and shortest path problems  Commodity distribution (flow)  Traffic control  Resource allocation  Geometric modeling  ...

Example Directed Graph (Digraph)

V = {a,b,c,d,e,f } E = {(a,b), (a,c), (a,e), (b,c), (b,d), (b,e), (c,d), (c,f), (d,e), (d,f), (e,f)} |V| = 6, |E| = 11 b a c d e f

Example Undirected Graph

An undirected graph is just like a directed graph, except the edges are unordered pairs ( sets ) {u,v}

Example:

V = {a,b,c,d,e,f } E = {{a,b}, {a,c}, {a,e}, {b,c}, {b,d}, {b,e}, {c,d}, {c,f}, {d,e}, {d,f}, {e,f}} b a c d e f

More Graph Terminology

 path: sequence of adjacent vertexes  length of path: number of edges  simple path: no vertex is repeated simple path of length 2: (b, c, d) simple path of length 0: (b) not a simple path: (b, c, e, b, c, d) b c d e

More Graph Terminology

 cycle: path that ends at its beginning  simple cycle: only repeated vertex is its beginning/end  acyclic graph: graph with no cycles  dag: directed acyclic graph b c d e cycles: (b, c, e, b) (b, c, e, b, c, e, b) simple cycle: (c, e, b, c) graph shown is not a dag Question: is (d) a cycle? No. A cycle must have at least one edge

Is this a dag?

 Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears b a c d e f

Is this a dag?

 Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears b c d e f

Is this a dag?

 Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears d e f

Is this a dag?

 Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears e f

Topological Sort

 We just computed a topological sort of the dag

This is a numbering of the vertices such that all edges go

from lower- to higher-numbered vertices

 Useful in job scheduling with precedence constraints

Coloring of an undirected graph: an assignment of a color to each node such that no two adjacent vertices get the same color How many colors are needed to color this graph?

Graph Coloring

b a c d e f