Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Graphen - Startprojekt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Algorithmen und Datenstrukturen
Graphen - Startprojekt
Commits
dc56b06b
Commit
dc56b06b
authored
5 years ago
by
Martin Schmollinger
Browse files
Options
Downloads
Patches
Plain Diff
added implementation of undirected graph
parent
570d460e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/io/ad/graphs/Graph.java
+177
-0
177 additions, 0 deletions
src/main/java/io/ad/graphs/Graph.java
with
177 additions
and
0 deletions
src/main/java/io/ad/graphs/Graph.java
+
177
−
0
View file @
dc56b06b
package
io.ad.graphs
;
import
java.util.*
;
/*
*
*/
public
class
Graph
{
// Kante
static
class
Edge
{
Node
dest
;
// Zielknoten
int
cost
;
// Kantengewicht
public
Edge
(
Node
n
,
int
c
)
{
dest
=
n
;
cost
=
c
;
}
public
Node
getDestNode
()
{
return
dest
;
}
public
int
getCost
()
{
return
cost
;
}
}
// Knoten
static
class
Node
{
String
label
;
ArrayList
<
Edge
>
adjList
=
new
ArrayList
<
Edge
>();
public
Node
(
String
s
)
{
label
=
s
;
}
public
String
toString
()
{
return
label
;
}
public
String
getLabel
()
{
return
label
;
}
public
void
addEdge
(
Edge
e
)
{
adjList
.
add
(
e
);
}
public
Iterator
<
Edge
>
getEdges
()
{
return
adjList
.
iterator
();
}
public
Edge
getEdgeTo
(
Node
n
)
{
for
(
Edge
e
:
adjList
)
{
if
(
e
.
dest
.
equals
(
n
))
return
e
;
}
return
null
;
}
}
// Verzeichnis aller Knoten des Graphen
private
HashMap
<
String
,
Node
>
nodeSet
=
new
HashMap
<
String
,
Node
>();
public
Graph
()
{
}
public
Node
addNode
(
String
label
)
throws
RuntimeException
{
if
(
nodeSet
.
containsKey
(
label
))
throw
new
RuntimeException
(
"Node Already Defined!"
);
Node
n
=
new
Node
(
label
);
nodeSet
.
put
(
label
,
n
);
return
n
;
}
public
Node
getNode
(
String
label
)
throws
NoSuchElementException
{
Node
n
=
nodeSet
.
get
(
label
);
if
(
n
==
null
)
throw
new
NoSuchElementException
();
return
n
;
}
public
void
addEdge
(
String
src
,
String
dest
,
int
cost
)
{
Node
srcNode
=
getNode
(
src
);
Node
destNode
=
getNode
(
dest
);
srcNode
.
addEdge
(
new
Edge
(
destNode
,
cost
));
//Make Graph undirected!
destNode
.
addEdge
(
new
Edge
(
srcNode
,
cost
));
}
/* TODO: Homework
* Realize a method that checks whether two nodes are connected.
*/
public
boolean
checkConnection
(
String
node1
,
String
node2
)
{
if
(
node1
==
null
||
node2
==
null
)
throw
new
RuntimeException
();
return
nodeSet
.
get
(
node1
).
getEdgeTo
(
nodeSet
.
get
(
node2
))!=
null
;
}
/* TODO: Homework
* Realize a method that returns a list of neighbor-nodes.
* Get the adjList of node. Iterate over his Edges and
* get the label of each destination node. Store labels
* in a new List of Strings.
*/
public
List
<
String
>
getNeighbours
(
String
label
)
{
if
(
label
==
null
)
throw
new
RuntimeException
();
Node
node
=
nodeSet
.
get
(
label
);
List
<
String
>
neighbors
=
new
LinkedList
<>();
Iterator
<
Edge
>
it
=
node
.
getEdges
();
while
(
it
.
hasNext
())
{
neighbors
.
add
(
it
.
next
().
getDestNode
().
getLabel
());
}
return
neighbors
;
}
/*TODO: Homework
* Realize breadth-first search (BFS) using a the java.util.Queue-Interface
* and an appropriate implementation.
*
*/
public
void
bfs
(
String
start
)
{
//TODO Validieriung von start
Node
startNode
=
nodeSet
.
get
(
start
);
Queue
<
Node
>
queue
=
new
LinkedList
<>();
Map
<
Node
,
Boolean
>
visited
=
new
HashMap
<>();
nodeSet
.
values
().
forEach
(
node
->
{
visited
.
put
(
node
,
false
);});
queue
.
add
(
startNode
);
visited
.
put
(
startNode
,
true
);
while
(!
queue
.
isEmpty
())
{
Node
node
=
queue
.
remove
();
System
.
out
.
println
(
node
.
getLabel
());
List
<
String
>
neighbors
=
getNeighbours
(
node
.
getLabel
());
for
(
String
neighbor
:
neighbors
)
{
if
(!
visited
.
get
(
getNode
(
neighbor
)))
{
visited
.
put
(
getNode
(
neighbor
),
true
);
queue
.
add
(
getNode
(
neighbor
));
}
}
}
}
/*TODO: Homework
* Realize an iterative variant of depth-first search (DFS) using a LinkedList as a Stack.
*
*/
public
void
dfs
(
String
start
)
{
Node
startNode
=
nodeSet
.
get
(
start
);
LinkedList
<
Node
>
stack
=
new
LinkedList
<>();
Map
<
Node
,
Boolean
>
visited
=
new
HashMap
<>();
nodeSet
.
values
().
forEach
(
node
->
{
visited
.
put
(
node
,
false
);});
stack
.
add
(
0
,
startNode
);
// push on stack
visited
.
put
(
startNode
,
true
);
while
(!
stack
.
isEmpty
())
{
Node
node
=
stack
.
remove
(
0
);
// pop from stack
System
.
out
.
println
(
node
.
getLabel
());
List
<
String
>
neighbors
=
getNeighbours
(
node
.
getLabel
());
for
(
String
neighbor
:
neighbors
)
{
if
(!
visited
.
get
(
getNode
(
neighbor
)))
{
visited
.
put
(
getNode
(
neighbor
),
true
);
stack
.
add
(
0
,
getNode
(
neighbor
));
//push on stack
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment