implementation of graph theory algorithms-java tool kit · implementation of graph theory...

6
Implementation of Graph Theory Algorithms-Java Tool Kit S.Varun Department of Physical Science,Vavuniya Campus,University of Jaffna, [email protected] S. Krishnakumar Department of Physical Science, Vavuniya Campus,University of Jaffna, [email protected] ABSTRACT Graph theory is the study of graphs, mathematical structures used to model pairwise relations between objects from a cer- tain collection. Many researchers involve in developing this theory and try to simply the proof of many graph theory theo- rems. This theory, however, useful for graphics programmers, computer scientist. But programmers like algorithm rather than understand theorems. There are several graph theory al- gorithms to solve the real world problems such as shortest path, map coloring,etc. Teaching and learning graph theory algorithms is a great challenge to both instructors and stu- dents. Instructors are seeking software that is specifically de- signed to demonstrate the algorithms and for students to learn these algorithms efficiently. To this problem, an attempt is made to develop a JAVA tool which generate the graphs and apply the graph theory algorithms. The tool should be read- ily available and provide an environment so that students are able to review the algorithm, solve a practical problem, and intuitively study the working process via a graphical display all together. This tool allows to create graphs using console and graphical manner. This package has user friendly tool and carry to demonstrable for students. Author Keywords Graph theory;Graph Algorithm;Java tool;Drawing. INTRODUCTION Graph theory has application in many areas like Mathematics, Computer Scince, Engineering, Comunications Scinect etc. The major developments of graph theory occurred by the ever growing importance of Computer Science and its connection with graph theory. There are several software tools are available in graph the- ory implementation. Some of them are JGRAPGT, GRAPH MAGIC IAPPGA and etc.Some of them are commercial pur- pose and some of them are developed in proprietor languages. In many universities in the world introduce the Graph the- ory course module for their computer science or mathematics undergraduate students. However, computer science students face difficulties to understand the graph theory because higher level mathematics knowledge is required to understand the theory. Thus, teaching this theory course by usual methods in a class room is not satisfied for computer science students un- less the students have good knowledge in higher level math- ematics. To over come this issue, in this paper, we introduce a portable teaching tool for Graph Theory. In the literacy sur- vey, it is clear that there are no such portable tools available to support the study of graph theory. This tool is developed us- ing JAVAwhich generate various type of graphs using graph theory algorithms. And also it gives a clear idea about the graph theory and it’s algorithms. Students and Demonstra- tors can use this software to understand and demonstrate the graph theory concepts.In this first version of the tool, we im- plemented and tested most popular algorithms of graph the- ory. Addition to that, tool can use not only the teaching pur- pose but also solve the real world applications associated with graph theory. Notations A graph G is a pair of sets V and E together with a function f : E V × V . The elements of V are the vertices (i.e. nodes or points) of G. The elements of E are the edges of G. The function f sends an edge to the pair of vertices that are its endpoints, thus f is known as the edgeendpoint function. This terminology is unfortunate since f is generally only a relation. Undirected and Directed graphs Connections generally come in two forms, those that are non- directional (e.g. the bridges of K¨ onigsberg) and those that have an implicit direction (e.g. the utility hookups water flows from the utility to ones home, not vice versa). To dis- tinguish these two cases two different kinds of graphs are to be defined. Weighted graph Another type of graph classification is weighted graphs.If each edges have the weight (e.g. cost, length, time, etc.) on the graph G,is called as weighed graph. BASIC DEFINITIONS The vertex set of a graph G is denoted by V (G), and the edge set is denoted by E(G). We may refer to these sets simply as V and E if the context makes the particular graph clear. For notational convenience, instead of representing an edge as u, v, we denote this simply by uv. Adjacent and Nonadjacent Given two vertices u and v, if uv E, then u and v are said to be adjacent. In this case, u and v are said to be the Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission. Copyright 2011 Faculty of Applied Science of the Vavuniya Campus of the University of Jaffna.

Upload: ngocong

Post on 24-Apr-2019

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Implementation of Graph Theory Algorithms-Java Tool Kit · Implementation of Graph Theory Algorithms-Java Tool Kit S.Varun Department of Physical Science,Vavuniya Campus,University

Implementation of Graph Theory Algorithms-Java Tool KitS.Varun

Department of Physical Science,VavuniyaCampus,University of Jaffna,

[email protected]

S. KrishnakumarDepartment of Physical Science, Vavuniya

Campus,University of Jaffna,[email protected]

ABSTRACTGraph theory is the study of graphs, mathematical structuresused to model pairwise relations between objects from a cer-tain collection. Many researchers involve in developing thistheory and try to simply the proof of many graph theory theo-rems. This theory, however, useful for graphics programmers,computer scientist. But programmers like algorithm ratherthan understand theorems. There are several graph theory al-gorithms to solve the real world problems such as shortestpath, map coloring,etc. Teaching and learning graph theoryalgorithms is a great challenge to both instructors and stu-dents. Instructors are seeking software that is specifically de-signed to demonstrate the algorithms and for students to learnthese algorithms efficiently. To this problem, an attempt ismade to develop a JAVA tool which generate the graphs andapply the graph theory algorithms. The tool should be read-ily available and provide an environment so that students areable to review the algorithm, solve a practical problem, andintuitively study the working process via a graphical displayall together. This tool allows to create graphs using consoleand graphical manner. This package has user friendly tooland carry to demonstrable for students.

Author KeywordsGraph theory;Graph Algorithm;Java tool;Drawing.

INTRODUCTIONGraph theory has application in many areas like Mathematics,Computer Scince, Engineering, Comunications Scinect etc.The major developments of graph theory occurred by the evergrowing importance of Computer Science and its connectionwith graph theory.

There are several software tools are available in graph the-ory implementation. Some of them are JGRAPGT, GRAPHMAGIC IAPPGA and etc.Some of them are commercial pur-pose and some of them are developed in proprietor languages.In many universities in the world introduce the Graph the-ory course module for their computer science or mathematicsundergraduate students. However, computer science studentsface difficulties to understand the graph theory because higher

level mathematics knowledge is required to understand thetheory. Thus, teaching this theory course by usual methods ina class room is not satisfied for computer science students un-less the students have good knowledge in higher level math-ematics. To over come this issue, in this paper, we introducea portable teaching tool for Graph Theory. In the literacy sur-vey, it is clear that there are no such portable tools available tosupport the study of graph theory. This tool is developed us-ing JAVA which generate various type of graphs using graphtheory algorithms. And also it gives a clear idea about thegraph theory and it’s algorithms. Students and Demonstra-tors can use this software to understand and demonstrate thegraph theory concepts.In this first version of the tool, we im-plemented and tested most popular algorithms of graph the-ory. Addition to that, tool can use not only the teaching pur-pose but also solve the real world applications associated withgraph theory.

NotationsA graph G is a pair of sets V and E together with a functionf : E → V × V . The elements of V are the vertices (i.e.nodes or points) of G. The elements of E are the edges of G.The function f sends an edge to the pair of vertices that areits endpoints, thus f is known as the edgeendpoint function.This terminology is unfortunate since f is generally only arelation.

Undirected and Directed graphsConnections generally come in two forms, those that are non-directional (e.g. the bridges of Konigsberg) and those thathave an implicit direction (e.g. the utility hookups waterflows from the utility to ones home, not vice versa). To dis-tinguish these two cases two different kinds of graphs are tobe defined.

Weighted graphAnother type of graph classification is weighted graphs.Ifeach edges have the weight (e.g. cost, length, time, etc.) onthe graph G,is called as weighed graph.

BASIC DEFINITIONSThe vertex set of a graph G is denoted by V (G), and the edgeset is denoted by E(G). We may refer to these sets simply asV and E if the context makes the particular graph clear. Fornotational convenience, instead of representing an edge as u,v, we denote this simply by uv.

Adjacent and NonadjacentGiven two vertices u and v, if uv ∈ E, then u and v aresaid to be adjacent. In this case, u and v are said to be the

Permission to make digital or hard copies of all or part of this work forpersonal or classroom use is granted without fee provided that copies arenot made or distributed for profit or commercial advantage and that copiesbear this notice and the full citation on the first page. To copy otherwise, orrepublish, to post on servers or to redistribute to lists, requires prior specificpermission.Copyright 2011 Faculty of Applied Science of the Vavuniya Campus of the University of Jaffna.

Page 2: Implementation of Graph Theory Algorithms-Java Tool Kit · Implementation of Graph Theory Algorithms-Java Tool Kit S.Varun Department of Physical Science,Vavuniya Campus,University

end vertices of the edge uv. If uv 6∈ E, then u and v arenonadjacent. Furthermore, if an edge e has a vertex v as anend vertex, we say that v is incident with e.

NeighborhoodThe neighborhood of a vertex v, denoted by N(v), is the setof vertices adjacent to v:

N(ν) = {x ∈ V |νx ∈ E}

DegreeThe degree of v, denoted by deg(v), is the number of edgesincident with v. In simple graphs, this is the same as the cardi-nality of the (open) neighborhood of v. The maximum degreeof a graph G, denoted by ∆(G) , is defined to be

∆(G) = max{deg(v)|vV (G)}Similarly, the minimum degree of a graph G, denoted byδ(G), is defined to be

δ(G) = min{deg(v)|vV (G)}

If G is directed graph,there are two types of degree such asindegree and outdegree.

METHODOLOGY: IMPLEMENTATION OF GRAPH ALGO-RITHMSThe tool saves the graph data as three maps (Java 6 inter-face). Those contains vertices, adjacent vertices, weight ofthe edges. According to this graph data, the tool can drawthe graph in the user interface panel. After that the user canapply the algorithms for the particular graph. In this pack-age, we have used several technologies in java such as paral-lel processing, serialization,collections etc. Parallel process-ing uses to concurrency update for graphical representationof the graphs when user edit the graph. For this purpose, wehave used thread which allows parallel processing.

GRAPH ALGORITHMS

PathThe graph Pn is simply a path on n vertices. A walk ina graph is a sequence of (not necessarily distinct) verticesv1, v2, ..., vk such that vivi+1 ∈ E for i = 1, 2, . . . , k-1.Such a walk is sometimes called a v1−vk walk, and v1andvk

are the end vertices of the walk. If the vertices in a walk aredistinct, then the walk is called a path.

Algorithm for check the pathStack ←− {v1, v2, v3, v4..........vn}IF stack contain a vertex more than one time then return False

Else

V 1←− pop(Stack)

L1:WHILE STACK is NOT EMPTY V 2←− pop(Stack)

IFadjacent(V 1, V 2)ThenBool −→ True

ElseBool −→ False,Break

Goto L1

Return Bool

Complete graphIn the mathematical field of graph theory, a complete graphis a simple undirected graph in which every pair of distinctvertices is connected by a unique edge.

Algorithm for check the Complete graphIF G is connected

n←− No of vertices

e←− No of edges

IF e.= n(n− 1)÷ 2

Then graph is complete(kn)

Regular graphA graph G is regular if every vertex has the same degree. Gis said to be regular of degree r (or r-regular) if deg(v) = r forall vertices v in G. Complete graphs of order n are regular ofdegree n - 1, and empty graphs are regular of degree 0.

Algorithm for check the Regular graphStack←− vertices

Degree←− pop(Stack)

WHILE Stack 6= Empty

D←− pop(Stack)

IF D6= Dergee

Then False

Loop

Complement graphGiven a graph G, the complement of G, denoted by G, is thegraph whose vertex set is the same as that of G, and whoseedge set consists of all the edges that are not present in G.

Algorithm for check the Complement graphv ←− V (G)

e←− E(G)

V(G)←− v

E(G)←− e

Draw new graph with V(G), E(G)

Page 3: Implementation of Graph Theory Algorithms-Java Tool Kit · Implementation of Graph Theory Algorithms-Java Tool Kit S.Varun Department of Physical Science,Vavuniya Campus,University

Bipartite graphA graph G is bipartite if its vertex set can be partitioned intotwo sets X and Y in such a way that every edge of G has oneend vertex in X and the other in Y . In this case, X and Yare called the partite sets. The first two graphs in Figure ??are bipartite. Since it is not possible to partition the verticesof the third graph into two such sets, the third graph is notbipartite. A bipartite graph with partite sets X and Y is calleda complete bipartite graph if its edge set is of the form E ={xy|x ∈ X, y ∈ Y }.

Algorithm for check the Bipartite graphV ←− V (G)

X ←− Null

Y ←− Null

X ←− maxdegreevertex(G)

X ←− Q | degree(Q) = 0

Do

AX ←− adjecent(X)

IF AX ∩X 6= Ø

BREAK

Else Y ←− AX

Remove Duplicate in Y

End IF

AY ←− adjecent(Y )

IF AY ∩ Y 6= Ø

BREAK

Else X ←− AY

Remove Duplicate in X

End IF

UNTIL | X | + | Y |=| V |

SubgraphGiven a graph and a subset S of the vertex set, the subgraphof G induced by S, denoted 〈S〉, is the subgraph with vertexset S and with edge set {uv|u, v ∈ Sanduv ∈ E(G). So,〈S〉 contains all vertices of S and all edges of G whose endvertices are both in S.

Vertex deletionGiven a graph G and a vertex v ∈V (G), we let G - v denotethe graph obtained by removing v and all edges incident withv from G. If S is a set of vertices, we let G - S denote thegraph obtained by removing each vertex of S and all associ-ated incident edges.

Edge deletionIf e is an edge of G, then G − eis the graph obtained by re-moving only the edge e (its end vertices stay). If T is a set

of edges, then G − T is the graph obtained by deleting eachedge of T from G

TreeA graph G is a tree when it is connected and contain no cycle.

Tree algorithmA graph of order n is a tree if and only if it is connected andcontains n− 1 edges.

IMPLEMENTATION TOOL KIT

MAIN INTERFACEThis tool is developed using netbeans IDE with Java6 tech-nology. The main window of the tool as shown in the Fig-ure 1.There are menu bar, property panel, tool box and com-mand window in the main window.

Figure 1. Main menu

In the new graph window,there is a check box in the left uppercorner. If you need weighed graph,tick that check box. Afterthat tool allow to draw the graph on that pad.There are twoway to draw the graph.

Draw the graph by using mouse• Select the ”Vertex” on the tool box,when the vertex active,

that button will change the color.

• Then draw the vertices by click on the pad. Select the”Edge” on the tool box,when the edge active, that buttonwill change the color.

• Then draw the edge by press on the start vertex and dragthe mouse point to the end vertex.

• If we need to change the position, the click ”Move” anddrag and drop the vertex to the new position.

All the activities display in the command window, as shownin Figure 3

Page 4: Implementation of Graph Theory Algorithms-Java Tool Kit · Implementation of Graph Theory Algorithms-Java Tool Kit S.Varun Department of Physical Science,Vavuniya Campus,University

Figure 2. Drawing new graph by using mouse

Figure 3. Command window

property of the graph also displayed in the detail window.Onthat window “Name”,“Number of vertices”,”Number ofedges”,“Dimension”,“type” and “location” are displayed.TheDetail window shown in the Figure 4

Figure 4. Properties of the Graph

The Graph can be save automatically.If user need to save thegraph in the different location.goto ”File” and click ”Save as”and select the destination.similly we can create the directedgraph.The example of the digraph shown in Figure 5

Figure 5. Directed Graph

Draw by keyboardBy using the command window,we can draw the graph. Thistool has some syntax to draw the graph.

drawvertex(x,y)

drawedge(v1,v2)

rmvertex(v)

rmedge(v1v2)

where x,y are coordinate values and v1,v2,v are vertex names.

WEIGHTED GRAPHNormally all edges in the graph has the same weight 1. Ifwe need to add the different weight to the edges, click weightcheck box and select the edge by click the end vertices or-derly. Then a text box will appear in the middle of the edge.Then we can add the value for that and press ”Enter” key. Theweighted graph is shown in the Figure 6

Figure 6. weighted graph

REMOVING THE EDGES AND VERTICESAnother important action of drawing is Remove. If we needto remove the vertex,select ”Select” button and select the ver-tex and click the ”Remove” button. Like wise edge also re-move by selecting end vertices. Remove action is shown inthe Figure 7.

QUERYIn the menu bar,there is a item ”Graph” then click ”Query”.

A new Query wizard will be displayed. On that window thereare five tab panel is available. first tab is ”Path” which isshown in Figure 8.

Page 5: Implementation of Graph Theory Algorithms-Java Tool Kit · Implementation of Graph Theory Algorithms-Java Tool Kit S.Varun Department of Physical Science,Vavuniya Campus,University

Figure 7. Removing vertex

Figure 8. Query-Path wizard

Click on that text box and select the vertices on the drawingpad. then click the button. Then answer will appear.simallyall other four panel can operate.

COMPLEMENTComplement graph is obtain by click ”graph” and click”Complement”. It will be appear in separate pad. Then wecan save the graph as ”file”.

SUBGRAPHFirst select the vertices on the graph.Then click ”graph” and”subgraph”.It will be appear in separate pad.Then we can savethe graph as ”file”.

Figure 9. Subgraph

SPANNING TREEFirst select the vertices on the graph.Then click ”graph” and”spaning Tree”.It will be appear in separate pad.Then we cansave the graph as ”file”.

VERTEX DETECTIONThis tool can input the graph using image files. It can beconvert as graph by color detraction.

Figure 10. Spanning Tree

Figure 11. Importing image

SAVE FILEWhen user save the graph,it will be save as object and inthe particular directory,two files will be created as .file for-mat.Those are shown in the Figure 13.

LOAD FILEWe can load the graph file simply by click ”file” and ”load”.

other wise there is a shortcut key ctrl + L.

In the particular location there may be two files. For example:If file name is Newgraph01. Then there will be other two filesnamed as Newgraph01 and Newgraph01$$. We have to openNewgraph01.file.

SHORT CUTThere following short keys are included in this tool

New Graph ctrl + N

Load ctrl + L

Save ctrl + S

Refresh F5

Select alt + S

Vertex alt + V

Edge alt + E

Move alt + M

Query alt + Q

Page 6: Implementation of Graph Theory Algorithms-Java Tool Kit · Implementation of Graph Theory Algorithms-Java Tool Kit S.Varun Department of Physical Science,Vavuniya Campus,University

Figure 12. Detecting vertices

Figure 13. Save file format

CONCLUSIONSeveral important definitions and theorems of the graph the-ory are formulated in the algorithmic format and imple-mented them in this tool. This tool allow users to draw, edit,modify(removing edges, vertices, changing the location ofnodes) any kind of graph or digraph and also it has savingand retrieving features. Obtaining subgraph from a graph ispossible in this tool. Specially, categorization of a graph(spanning tree,bipartite graph, complete graph etc..) is alsopossible which is important to analyse the properties of any

graph. In addition, user may load a image file of a graph andtool can detect the vertices of the images. It gives the actualresults for the queries for included algorithms. This tool willhelp to students to understand the graph theory concepts. Infuture, all other algorithms and relevant graph theories( suchas Shortest path, 4 color problem, planar tree etc..) will beincluded in this tool.

ACKNOWLEDGMENTSAuthors wish to thank staff members of the departmentof Physical Science, Faculty of Applied Science, VavuniyaCampus for their support encouragement.

REFERENCES1. O Reilly and Associates, Java Swing, 1998.

2. Java graphics, available at:http://[email protected]

3. John M. Harris Jeffry L. Hirst Michael J. Mossinghoff,Combinatorics and Graph Theory. Second Edition

4. S. Krishanakumar, Lecture notes on Combinatorics andGraph Theory,Deparment of Physical Science, VavuniyaCampus, Sri Lanka, 2010.

5. David Joyner, Minh Van Nguyen, Nathann Cohen,Algorithmic Graph Theory. Version 0.7-r1901 2011September 23

6. Bruce Hoppe , Introduction to Network Mathematics.

7. Graph Theory notes, available at:http://www.math.utah.edu/mathcircle/notes/

8. Graph Theory notes, available at:http://mathworld.wolfram.com