Graphs Tessema M. Mengistu Department of Computer Science Southern - - PowerPoint PPT Presentation

graphs
SMART_READER_LITE
LIVE PREVIEW

Graphs Tessema M. Mengistu Department of Computer Science Southern - - PowerPoint PPT Presentation

Graphs Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - Faner 3131 1 Outline Introduction to Graphs Graph Traversals Finding a Path Graph ADT 2


slide-1
SLIDE 1

Graphs

1

Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - Faner 3131

slide-2
SLIDE 2

Outline

  • Introduction to Graphs
  • Graph Traversals
  • Finding a Path
  • Graph ADT

2

slide-3
SLIDE 3

Introduction to Graph

  • There are many applications that uses graphs

– Google Map

3

slide-4
SLIDE 4

Introduction to Graph

  • Airline Reservation Systems

4

slide-5
SLIDE 5

Introduction to Graph

  • Global Positioning System (GPS)

5

slide-6
SLIDE 6

Introduction to Graph

  • Graph

– Consists of a finite set (distinct) of vertices or nodes or points that are connected together using lines called edges – A subgraph is a portion of a graph that is itself a graph

6

slide-7
SLIDE 7

Introduction to Graph

7

slide-8
SLIDE 8

Graph Terminology

  • Nodes connected by edges
  • Edges

– Undirected – Directed (digraph)

slide-9
SLIDE 9

Graph Terminology

  • Path between two vertices

– Sequence of edges

  • A path in a directed graph must consider the

direction of the edges, and is called a directed path.

  • The length of a path is the number of edges that

it comprises.

  • If the path does not pass through any vertex

more than once, it is a simple path.

  • Graph can be:

– Cyclic – has a path that begins and ends at the same vertex – Acyclic - A graph that has no cycles is

9

slide-10
SLIDE 10

Graph Terminology

  • Weights

– Shortest, fastest, cheapest, costs

  • A weighted graph

– has values on its edges – E.g distance in miles, cost, time of driving, etc

  • A path in a weighted graph has a weight, or

cost, which is the sum of its edge weights

10

slide-11
SLIDE 11

Graph Terminology

  • Connected graphs

– A graph that has a path between every pair of distinct vertices

  • A complete graph

– Has an edge between every pair of distinct vertices.

11

slide-12
SLIDE 12

Graph Terminology

  • Adjacent vertices

– Undirected graph

  • Two vertices are adjacent if they are joined by an edge
  • Also are called neighbors

– Directed graph

  • Vertex i is adjacent to vertex j if a directed edge begins

at j and ends at i.

  • Vertex A is adjacent to vertex B, but vertex B is not

adjacent to vertex A.

12

slide-13
SLIDE 13

Graph Terminology

  • If a graph has n vertices, it can have at most

– n (n - 1) edges if the graph is directed – n (n - 1) / 2 edges if the graph is undirected

  • A graph is sparse if it has relatively few edges

– Has O(n) edges

  • A graph is dense if it has many edges

– Has O(n2) edges.

  • Typical graphs are sparse.

13

slide-14
SLIDE 14

Graphs Vs Trees

  • All trees are graphs, but not all graphs are

trees.

  • A tree is a connected graph without cycles.

14

slide-15
SLIDE 15

Graph Traversals

  • Graph Traversals

– Focus on the connections between vertices, rather than the contents of vertices

  • In a graph, “visit a node” means simply to

“mark the node as visited.”

  • Begins at any vertex—called the origin

vertex— and visits only the vertices that it can reach.

  • Two types

– Breadth-first – Depth-first

15

slide-16
SLIDE 16

Breadth-first Traversal

  • Breadth-first traversal

– Visits all neighbors of a node before visiting the neighbors’ neighbors.

  • Traversal uses a queue to hold the visited

vertices.

  • The traversal order is then the order in which

vertices are added to the queue.

16

slide-17
SLIDE 17

Breadth-first Traversal

17

slide-18
SLIDE 18

18

slide-19
SLIDE 19

Breadth-first Traversal

  • Example

19

slide-20
SLIDE 20

Depth-first Traversal

  • Depth-first traversal

– Follows a path that goes as deeply into the graph as possible before following other paths. – After visiting a vertex, this traversal visits the vertex’s neighbor, the neighbor’s neighbor, and so

  • n.

– Uses a stack in the iterative description of this traversal.

20

slide-21
SLIDE 21

Depth-first Traversal

21

slide-22
SLIDE 22

22

slide-23
SLIDE 23

Depth-first Traversal

  • Example

23

slide-24
SLIDE 24

Path

  • Path examples

– Learning whether a particular airline flies between two given cities – Finding if there is a road connecting two cities

  • A depth-first traversal stays on a path through the

graph as it visits as many vertices as possible.

– Each time we visit another vertex, we see whether that vertex is the desired destination. – If so, we are done and the resulting stack contains the path. – Otherwise, we continue the traversal until either we are successful or the traversal ends.

24

slide-25
SLIDE 25

Shortest Path – unweighted Graph

  • A graph can have several different paths

between the same two vertices.

  • We can find the path with the shortest length,

that is, the path that has the fewest edges.

– Example – a path between A and H

25

slide-26
SLIDE 26

Algorithm

  • Every node keeps

– Its predecessor node – A length of the path to reach that specific node – Example

26

slide-27
SLIDE 27

27

slide-28
SLIDE 28

28

slide-29
SLIDE 29

Shortest Path – Weighted Graph

  • The shortest path is not necessarily the one

with the fewest edges.

  • The Shortest path is the one with the smallest

edge-weight sum.

  • Example

29

slide-30
SLIDE 30

Figure 28-19 A trace of the traversal in the algorithm to find the cheapest path from vertex A to vertex H in a weighted graph

slide-31
SLIDE 31

Figure 28-19 A trace of the traversal in the algorithm to find the cheapest path from vertex A to vertex H in a weighted graph

slide-32
SLIDE 32

FIGURE 28-20 The graph in Figure 28-18a after finding the cheapest path from vertex A to vertex H

slide-33
SLIDE 33

33

slide-34
SLIDE 34

34

slide-35
SLIDE 35

Graph ADT

  • No addition, removal, or retrieval

components.

  • We use a graph to answer questions based on

the relationships among its vertices.

35

slide-36
SLIDE 36
  • Example

36