ECE 242 Data Structures Lecture 28 Introduction to Graphs - - PDF document

ece 242 data structures
SMART_READER_LITE
LIVE PREVIEW

ECE 242 Data Structures Lecture 28 Introduction to Graphs - - PDF document

ECE 242 Data Structures Lecture 28 Introduction to Graphs November 20, 2009 ECE242 L28: Introduction to Graphs Overview Problem: How do we represent irregular connections between locations? Graphs Definition Directed and


slide-1
SLIDE 1

ECE242 L28: Introduction to Graphs November 20, 2009

ECE 242 Data Structures

Lecture 28

Introduction to Graphs

ECE242 L28: Introduction to Graphs November 20, 2009

Overview °Problem: How do we represent irregular connections between locations? °Graphs

  • Definition
  • Directed and Undirected graph
  • Simple path and cycle
  • Connected and Unconnected graph
  • Weighted graph

°Graph Representation °Graph Traversal

slide-2
SLIDE 2

ECE242 L28: Introduction to Graphs November 20, 2009

Abstract Data Type °We have discussed:

  • List
  • Tree
  • Today we will talk about Graphs

ECE242 L28: Introduction to Graphs November 20, 2009

Northwest Airline Flight Plan Boston Hartford Atlanta Minneapolis Austin SF Seattle Anchorage

slide-3
SLIDE 3

ECE242 L28: Introduction to Graphs November 20, 2009

Computer Network Or Internet MCI Regional Network Intel Campus Umass AT&T

ECE242 L28: Introduction to Graphs November 20, 2009

Lots of Interesting Problems °Graph traversal °Shortest path between two nodes °Anything else?

slide-4
SLIDE 4

ECE242 L28: Introduction to Graphs November 20, 2009

Concepts of Graphs edges (weight) node or vertex

ECE242 L28: Introduction to Graphs November 20, 2009

Graph Definition °G = (V, E) °V is the vertex set

  • Vertices are also called nodes or points

°E is the edge set

  • Each edge connects two vertices
  • Edges are also called arcs or lines
slide-5
SLIDE 5

ECE242 L28: Introduction to Graphs November 20, 2009

Graphs, Vertices and Edges

ECE242 L28: Introduction to Graphs November 20, 2009

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

slide-6
SLIDE 6

ECE242 L28: Introduction to Graphs November 20, 2009

Subgraph °Subgraph:

  • subset of vertices and edges

ECE242 L28: Introduction to Graphs November 20, 2009

Simple Path °A simple path traverse a node no more than once

  • ABCD is a simple path

B C D A path

slide-7
SLIDE 7

ECE242 L28: Introduction to Graphs November 20, 2009

Cycle °A cycle is a path that starts and ends at the same point

  • CBDC is a cycle

B C D A

ECE242 L28: Introduction to Graphs November 20, 2009

Connected vs. Unconnected Graph Connected Graph Unconnected Graph

slide-8
SLIDE 8

ECE242 L28: Introduction to Graphs November 20, 2009

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

  • Course Requirement Graph: DAG

ECE242 L28: Introduction to Graphs November 20, 2009

Directed Acyclic Graph D C B A °This is not DAG

  • ABCD is a cycle
slide-9
SLIDE 9

ECE242 L28: Introduction to Graphs November 20, 2009

Weighted Graph

° Weighted graph: a graph with numbers assigned to its edges ° Weight: cost, distance, travel time, hop, etc.

1 3 2 20 10 1 5 4

ECE242 L28: Introduction to Graphs November 20, 2009

Representation Of Graph °Two representations

  • Adjacency Matrix
  • Adjacency List
slide-10
SLIDE 10

ECE242 L28: Introduction to Graphs November 20, 2009

Adjacency Matrix °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

ECE242 L28: Introduction to Graphs November 20, 2009

Example of Adjacency Matrix 1 3 2

A[i][j] 1 2 3 1 1 1 1 1 1 2 1 1 1 3 1 1

So, Matrix A =

0 1 1 0 1 0 1 1 1 1 0 1 0 1 1 0

slide-11
SLIDE 11

ECE242 L28: Introduction to Graphs November 20, 2009

Undirected vs. Directed °Undirected graph

  • adjacency matrix is symmetric
  • A[i][j] always equals A[j][i]

°Directed graph

  • adjacency matrix may not be symmetric
  • A[i][j] may not equal A[j][i]

ECE242 L28: Introduction to Graphs November 20, 2009

Directed Graph Matrix Representation

A[i][j] 1 2 3 1 1 1 1 1 2 1 3

1 3 2 So, Matrix A =

0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0

slide-12
SLIDE 12

ECE242 L28: Introduction to Graphs November 20, 2009

Weighted Graph

A[i][j] 1 2 3 20 10 1 1 20 5 2 10 4 3 1 5 4

2 3 1 20 10 1 5 4 So, Matrix A =

0 20 10 1 20 0 0 5 10 0 0 4 1 5 4 0

ECE242 L28: Introduction to Graphs November 20, 2009

Adjacency Matrices

slide-13
SLIDE 13

ECE242 L28: Introduction to Graphs November 20, 2009

Adjacency List °An array of list °the ith element of array is a list of vertices that connect to vertex i

ECE242 L28: Introduction to Graphs November 20, 2009

Example of Adjacency List 1 3 2 1 2 3 1 2 3 3 3 vertex 0 connect to vertex 1, 2 and 3 vertex 1 connects to 3 vertex 2 connects to 3

slide-14
SLIDE 14

ECE242 L28: Introduction to Graphs November 20, 2009

Weighted Graph

° Weighted graph: extend each node with an addition field: weight

1 3 2 20 10 1 5 4 1 2 3 1 10 2 20 3 1 0 10 3 4 0 20 3 5 0 1 1 4 2 5

ECE242 L28: Introduction to Graphs November 20, 2009

Adjacency List

slide-15
SLIDE 15

ECE242 L28: Introduction to Graphs November 20, 2009

Compare Two Representations °Given two vertices: u, v

  • find out if u and v are adjacent

°Given a vertex: u

  • enumerate all neighbors of u

°For all vertices

  • enumerate all neighbors of each vertex

ECE242 L28: Introduction to Graphs November 20, 2009

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 O(N) degree of node O(N) For all vertices: enumerate all neighbors of each vertex O(N2) Summations of all node degree O(E)

slide-16
SLIDE 16

ECE242 L28: Introduction to Graphs November 20, 2009

Complete Graph

Total number of edges in graph: E = N(N-1)/2 = O(N2)

  • There is an edge between any two vertices

ECE242 L28: Introduction to Graphs November 20, 2009

Sparse Graph For example: E = N-1= O(N)

  • There are very small no. of edges in the graph
slide-17
SLIDE 17

ECE242 L28: Introduction to Graphs November 20, 2009

Space Requirement of Representations °Memory space:

  • adjacency matrix

O(N2)

  • adjacency list

O(E)

°Sparse graph

  • adjacency list is better

°Dense graph

  • same running time

ECE242 L28: Introduction to Graphs November 20, 2009

Summary °We will spend the rest of the semester discussing graphs °Many important problems represented as graphs

  • Mapquest, chip routing, Google, maze type games

°Next step: search for a node in a graph °Other problem: shortest path to node, shortest path to all nodes