Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Tree Algorithms
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Introduction to Graph Theory
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Graphs Upon completion you will be able to:
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Theory Graph Theory - History Leonhard Euler's paper on “Seven Bridges of Königsberg”, published in 1736.
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CS 3343: Analysis of Algorithms
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
CSE 373 Data Structures and Algorithms
Minimum-Cost Spanning Tree
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Minimum Spanning Tree.
CSE 373: Data Structures and Algorithms
Graphs G = (V, E) V are the vertices; E are the edges.
Minimum spanning trees
Minimum Spanning Trees (MSTs)
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Algorithm Course Dr. Aref Rashad
Lecture 10 Graph Algorithms
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Graph Dr. Bernard Chen Ph.D. University of Central Arkansas

Graph Algorithms Graphs and Theorems about Graphs Graph Algorithms minimum spanning tree

What can graphs model? Cost of wiring electronic components together. Shortest route between two cities. Finding the shortest distance between all pairs of cities in a road atlas.

What is a Graph? Informally a graph is a set of nodes joined by a set of lines or arrows

Directed Graph A directed graph is a pair ( V, E ), where the set V is a finite set and E is a binary relation on V. The set V is called the vertex set of G and the elements are called vertices. The set E is called the edge set of G and the elements are edges

Directed Graph V = { 1, 2, 3, 4, 5, 6, 7 } | V | = 7 E = { (1,2), (2,2), (2,4), (4,5), (4,1), (5,4),(6,3) } | E | = 7 Self loop 7 Isolated node

Undirected Graph An undirected graph G = ( V, E ), but unlike a digraph the edge set E consist of unordered pairs. We use the notation (a, b ) to refer to a directed edge, and { a, b } for an undirected edge.

Undirected Graph A D E F B C V = { A, B, C, D, E, F } |V | = 6 E = { {A, B}, {A,E}, {B,E}, {C,F} } |E | = 4 Some texts use (a, b) also for undirected edges. So ( a, b ) and ( b, a ) refers to the same edge.

Degree Degree of a Vertex in an undirected graph is the number of edges incident on it. In a directed graph, the out degree of a vertex is the number of edges leaving it and the in degree is the number of edges entering it.

Degree A D E F B C The degree of B is 2.

Degree The in degree of 2 is 2 and the out degree of 2 is 3.

Weighted Graph A weighted graph is a graph for which each edge has an associated weight, usually given by a weight function w: E  R.

Weighted Graph

Implementation of a Graph Adjacency-list representation of a graph G = ( V, E ) consists of an array ADJ of |V | lists, one for each vertex in V. For each u  V, ADJ [ u ] points to all its adjacent vertices.

Implementation of a Graph

Adjacency lists Advantage: Saves space for sparse graphs. Most graphs are sparse. “Visit” edges that start at v Must traverse linked list of v Size of linked list of v is degree(v)  (degree(v))

Adjacency-matrix- representation Adjacency-matrix-representation of a graph G = ( V, E) is a |V | x |V | matrix A = ( a ij ) such that a ij = 1 (or some Object) if (i, j )  E 0 (or null) otherwise.

Adjacency-matrix- representation

Adjacency-matrix- representation

Adjacency-matrix- representation Advantage: Saves space on pointers for dense graphs, and on small unweighted graphs using 1 bit per edge. Check for existence of an edge (v, u) (adjacency [i] [j]) == true?) So  (1)

Graph Algorithms Graphs and Theorems about Graphs Graph Algorithms minimum spanning tree

Minimum Spanning Tree

Example of MST

Problem: Laying Telephone Wire Central office

Wiring: Naïve Approach Central office Expensive!

Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

Growing an MST: general idea GENERIC-MST(G,w) 1. A  {} 2. while A does not form a spanning tree 3. do find an edge (u,v) that is safe for A 4. A  A U {(u,v)} 5. return A

Tricky part How do you find a safe edge? This safe edge is part of the minimum spanning tree

Algorithms for MST Prim’s Grow a MST by adding a single edge at a time Kruskal’s Choose a smallest edge and add it to the forest If an edge is formed a cycle, it is rejected

Prim’s greedy algorithm Start from some (any) vertex. Build up spanning tree T, one vertex at a time. At each step, add to T the lowest-weight edge in G that does not create a cycle. Stop when all vertices in G are touched

Prim’s MST algorithm

Example A B D G C F I E H

Min Edge Pick a root A B D G C F I E H = in heap

Min Edge = 1 A B D G C F I E H

Min Edge = 2 A B D G C F I E H

A B D G C F I E H

Min Edge = 3 A B D G C F I E H

Min Edge = 4 A B D G C F I E H

Min Edge = 3 A B D G C F I E H

Min Edge = 4 A B D G C F I E H

Min Edge = 6 A B D G C F I E H

Example II

Kruskal’s Algorithm Choose the smallest edge and add it to a forest Keep connecting components until all vertices connected If an edge would form a cycle, it is rejected.

Example A B D G C F I E H

Min Edge = 1 A B D G C F I E H

Min Edge = 2 A B D G C F I E H

A B D G C F I E H

Min Edge = 3 A B D G C F I E H Now have 2 disjoint components: ABFG and CH

Min Edge = 3 A B D G C F I E H

Min Edge = 4 A B D G C F I E H Two components now merged into one.

Min Edge = 4 A B D G C F I E H

Min Edge = 5 A B D G C F I E H Rejected due to a cycle BFGB

Min Edge = 6 A B D G C F I E H