Graph.

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Chapter 8, Part I Graph Algorithms.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Graphs.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R5. Graphs.
Graph & BFS.
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.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
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 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
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.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
COSC 2007 Data Structures II
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 05 Introduction to Graph And Search Algorithms.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CS202 - Fundamental Structures of Computer Science II
Graphs Lecture 19 CS2110 – Spring 2013.
Graphs Representation, BFS, DFS
Basic Concepts Graphs For more notes and topics visit:
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graphs Chapter 11 Objectives Upon completion you will be able to:
CS223 Advanced Data Structures and Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs Chapter 7 Visit for more Learning Resources.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
GRAPHS.
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

Northwest Airline Flight Anchorage Boston Minneapolis Seattle Hartford SF Atlanta Austin

Computer Network Or Internet Charter Comcast Regional Network UNT Intel

Application Traveling Saleman Find the shortest path that connects all cities without a loop. Start

Concepts of Graphs node or vertex edges (weight)

Graph Definition a b c d e A graph G = (V,E) is composed of: V: set of vertices (nodes) E: set of edges (arcs) connecting the vertices in V An edge e = (u,v) is a pair of vertices Example: a b V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)} c d e

Undirected vs. Directed Graph Undirected Graph edge has no oriented Directed Graph edge has oriented vertex

Degree of a Vertex The degree of a vertex is the number of edges to that vertex For directed graph, the in-degree of a vertex v is the number of edges that have v as the head the out-degree of a vertex v is the number of edges that have v as the tail if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is Hint: Adjacent vertices are counted twice.

Subgraph Subgraph: subset of vertices and edges

Simple Path A simple path is a path such that all vertices are distinct, except that the first and the last could be the same. ABCD is a simple path B C A path D

Cycle A cycle is a path that starts and ends at the same point. For undirected graph, the edges are distinct. CBDC is a cycle B C A D

Connected vs. Unconnected Graph

Directed Acyclic Graph Directed Acyclic Graph (DAG) : directed graph without cycle

Weighted Graph Weighted graph: a graph with numbers assigned to its edges Weight: cost, distance, travel time, hop, etc. 10 20 1 1 2 4 5 3

Representation Of Graph Two representations Adjacency Matrix Adjacency List

Adjacency Matrix Assume N nodes in graph Use Matrix A[0…N-1][0…N-1] if vertex i and vertex j are adjacent in graph, A[i][j] = 1, otherwise A[i][j] = 0 if vertex i has a loop, A[i][i] = 1 if vertex i has no loop, A[i][i] = 0

Example of Adjacency Matrix A[i][j] 1 2 3 1 2 3 0 1 1 0 1 0 1 1 1 1 0 1 So, Matrix A =

Undirected vs. Directed Undirected graph adjacency matrix is symmetric A[i][j]=A[j][i] Directed graph adjacency matrix may not be symmetric A[i][j]A[j][i]

Directed Graph A[i][j] 1 2 3 1 2 3 So, Matrix A = 0 1 1 1 0 0 0 1 1 2 3 1 2 3 0 1 1 1 0 0 0 1 0 0 0 0 So, Matrix A =

Weighted Graph A[i][j] 1 2 3 20 10 5 4 10 20 1 1 2 4 5 3 1 2 3 20 10 5 4 10 20 1 1 2 0 20 10 1 20 0 0 5 10 0 0 4 1 5 4 0 4 5 3 So, Matrix A =

Adjacency List An array of list the ith element of the array is a list of vertices that connect to vertex i 1 2 3 1 2 3 vertex 0 connect to vertex 1, 2 and 3 vertex 1 connects to 3 vertex 2 connects to 3

Weighted Graph Weighted graph: extend each node with an addition field: weight 1 10 2 20 3 1 10 20 1 10 3 4 1 1 2 2 20 3 5 4 3 5 1 1 4 2 5 3

Comparison Of Representations Cost Adjacency Matrix Adjacency List Given two vertices u and v: find out whether u and v are adjacent O(1) degree of node O(N) Given a vertex u: enumerate all neighbors of u For all vertices: enumerate all neighbors of each vertex O(N2) Summations of all node degree O(E)

Complete Graph There is an edge between any two vertices Total number of edges in graph: E = N(N-1)/2 = O(N2)

Sparse Graph There is a very small number of edges in the graph For example: E = N-1= O(N)

Space Requirements Memory space: Sparse graph Dense graph adjacency matrix O(N2) adjacency list O(E) Sparse graph adjacency list is better Dense graph same running time

Graph Traversal List out all cities that United Airline can reach from Hartford Airport CHI Hartford SF NYC LA DC

Graph Traversal From vertex u, list out all vertices that can be reached in graph G Set of nodes to expand Each node has a flag to indicate visited or not

Traversal Algorithm Step 1: { Hartford } find neighbors of Hartford { Hartford, NYC, CHI } CHI Hartford SF NYC LA W. DC

Traversal Algorithm Step 2: { Hartford, NYC, CHI } find neighbors of NYC, CHI { Hartford, NYC, CHI, LA, SF } CHI Hartford SF NYC LA W. DC

Traversal Algorithm Step 3: {Hartford, NYC, CHI, LA, SF } find neighbors of LA, SF no other new neighbors CHI Hartford SF NYC LA W. DC

Traversal Algorithm Finally we get all cities that United Airline can reach from Hartford Airport {Hartford, NYC, CHI, LA, SF } CHI Hartford SF NYC LA W. DC

Algorithm of Graph Traversal Mark all nodes as unvisited Pick a starting vertex u, add u to probing list While ( probing list is not empty) { Remove a node v from probing list Mark node v as visited For each neighbor w of v, if w is unvisited, add w to the probing list }

Graph Traversal Algorithms Two algorithms Depth First Traversal Breadth First Traversal

Depth First Traversal Probing List is implemented as stack (LIFO) Example A’s neighbor: B, C, E B’s neighbor: A, C, F C’s neighbor: A, B, D D’s neighbor: E, C, F E’s neighbor: A, D F’s neighbor: B, D start from vertex A A B E C D F

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Initial State Visited Vertices { } Probing Vertices { A } Unvisited Vertices { A, B, C, D, E, F } stack A

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is A, mark it as visited Find A’s first unvisited neighbor, push it into stack Visited Vertices { A } Probing vertices { A, B } Unvisited Vertices { B, C, D, E, F } B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is B, mark it as visited Find B’s first unvisited neighbor, push it in stack Visited Vertices { A, B } Probing Vertices { A, B, C } Unvisited Vertices { C, D, E, F } C B B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is C, mark it as visited Find C’s first unvisited neighbor, push it in stack Visited Vertices { A, B, C } Probing Vertices { A, B, C, D } Unvisited Vertices { D, E, F } D C C B B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is D, mark it as visited Find D’s first unvisited neighbor, push it in stack Visited Vertices { A, B, C, D } Probing Vertices { A, B, C, D, E } Unvisited Vertices { E, F } E D D C C B B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is E, mark it as visited Find E’s first unvisited neighbor, no vertex found, Pop E Visited Vertices { A, B, C, D, E } Probing Vertices { A, B, C, D } Unvisited Vertices { F } E D D C C B B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is D, mark it as visited Find D’s first unvisited neighbor, push it in stack Visited Vertices { A, B, C, D, E } Probing Vertices { A, B, C, D, F} Unvisited Vertices { F } F D D C C B B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is F, mark it as visited Find F’s first unvisited neighbor, no vertex found, Pop F Visited Vertices { A, B, C, D, E, F } Probing Vertices { A, B, C, D} Unvisited Vertices { } F D D C C B B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is D, mark it as visited Find D’s first unvisited neighbor, no vertex found, Pop D Visited Vertices { A, B, C, D, E, F } Probing Vertices { A, B, C } Unvisited Vertices { } D C C B B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is C, mark it as visited Find C’s first unvisited neighbor, no vertex found, Pop C Visited Vertices { A, B, C, D, E, F } Probing Vertices { A, B } Unvisited Vertices { } C B B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is B, mark it as visited Find B’s first unvisited neighbor, no vertex found, Pop B Visited Vertices { A, B, C, D, E, F } Probing Vertices { A } Unvisited Vertices { } B A A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Peek a vertex from stack, it is A, mark it as visited Find A’s first unvisited neighbor, no vertex found, Pop A Visited Vertices { A, B, C, D, E, F } Probing Vertices { } Unvisited Vertices { } A stack

Depth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Now probing list is empty End of Depth First Traversal Visited Vertices { A, B, C, D, E, F } Probing Vertices { } Unvisited Vertices { } stack

Breadth First Traversal Probing List is implemented as queue (FIFO) Example A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D start from vertex A A B E C D F

Breadth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Initial State Visited Vertices { } Probing Vertices { A } Unvisited Vertices { A, B, C, D, E, F } A queue

Breadth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Delete first vertex from queue, it is A, mark it as visited Find A’s all unvisited neighbors, mark them as visited, put them into queue Visited Vertices { A, B, C, E } Probing Vertices { B, C, E } Unvisited Vertices { D, F } A B C E queue

Breadth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Delete first vertex from queue, it is B, mark it as visited Find B’s all unvisited neighbors, mark them as visited, put them into queue Visited Vertices { A, B, C, E, F } Probing Vertices { C, E, F } Unvisited Vertices { D } B C E C E F queue

Breadth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Delete first vertex from queue, it is C, mark it as visited Find C’s all unvisited neighbors, mark them as visited, put them into queue Visited Vertices { A, B, C, E, F, D } Probing Vertices { E, F, D } Unvisited Vertices { } C E F E F D queue

Breadth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Delete first vertex from queue, it is E, mark it as visited Find E’s all unvisited neighbors, no vertex found Visited Vertices { A, B, C, E, F, D } Probing Vertices { F, D } Unvisited Vertices { } E F D F D queue

Breadth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Delete first vertex from queue, it is F, mark it as visited Find F’s all unvisited neighbors, no vertex found Visited Vertices { A, B, C, E, F, D } Probing Vertices { D } Unvisited Vertices { } F D D queue

Breadth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Delete first vertex from queue, it is D, mark it as visited Find D’s all unvisited neighbors, no vertex found Visited Vertices { A, B, C, E, F, D } Probing Vertices { } Unvisited Vertices { } D queue

Breadth First Traversal (Cont) A’s neighbor: B C E B’s neighbor: A C F C’s neighbor: A B D D’s neighbor: E C F E’s neighbor: A D F’s neighbor: B D A B E C D F Now the queue is empty End of Breadth First Traversal Visited Vertices { A, B, C, E, F, D } Probing Vertices { } Unvisited Vertices { } queue

Difference Between DFT & BFT Depth First Traversal (DFT) order of visited: A, B, C, D, E, F Breadth First Traversal (BFT) order of visited: A, B, C, E, F, D A B E C D F